1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0 3# This is not an autoconf generated configure 4 5INCLUDE="$PWD/include" 6PREFIX="/usr" 7LIBDIR="\${prefix}/lib" 8 9# Output file which is input to Makefile 10CONFIG=config.mk 11 12# Make a temp directory in build tree. 13TMPDIR=$(mktemp -d config.XXXXXX) 14trap 'status=$?; rm -rf $TMPDIR; exit $status' EXIT HUP INT QUIT TERM 15 16check_toolchain() 17{ 18 : ${PKG_CONFIG:=pkg-config} 19 : ${AR=ar} 20 : ${CC=gcc} 21 : ${YACC=bison} 22 echo "PKG_CONFIG:=${PKG_CONFIG}" >>$CONFIG 23 echo "AR:=${AR}" >>$CONFIG 24 echo "CC:=${CC}" >>$CONFIG 25 echo "YACC:=${YACC}" >>$CONFIG 26} 27 28check_atm() 29{ 30 cat >$TMPDIR/atmtest.c <<EOF 31#include <atm.h> 32int main(int argc, char **argv) { 33 struct atm_qos qos; 34 (void) text2qos("aal5,ubr:sdu=9180,rx:none",&qos,0); 35 return 0; 36} 37EOF 38 39 if $CC -I$INCLUDE -o $TMPDIR/atmtest $TMPDIR/atmtest.c -latm >/dev/null 2>&1; then 40 echo "TC_CONFIG_ATM:=y" >>$CONFIG 41 echo yes 42 else 43 echo no 44 fi 45 rm -f $TMPDIR/atmtest.c $TMPDIR/atmtest 46} 47 48check_xtables() 49{ 50 if ! ${PKG_CONFIG} xtables --exists; then 51 echo "TC_CONFIG_NO_XT:=y" >>$CONFIG 52 fi 53} 54 55check_xt() 56{ 57 #check if we have xtables from iptables >= 1.4.5. 58 cat >$TMPDIR/ipttest.c <<EOF 59#include <xtables.h> 60#include <linux/netfilter.h> 61static struct xtables_globals test_globals = { 62 .option_offset = 0, 63 .program_name = "tc-ipt", 64 .program_version = XTABLES_VERSION, 65 .orig_opts = NULL, 66 .opts = NULL, 67 .exit_err = NULL, 68}; 69 70int main(int argc, char **argv) 71{ 72 xtables_init_all(&test_globals, NFPROTO_IPV4); 73 return 0; 74} 75EOF 76 77 if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL \ 78 $(${PKG_CONFIG} xtables --cflags --libs) -ldl >/dev/null 2>&1; then 79 echo "TC_CONFIG_XT:=y" >>$CONFIG 80 echo "using xtables" 81 fi 82 rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest 83} 84 85check_xt_old() 86{ 87 # bail if previous XT checks has already succeeded. 88 grep -q TC_CONFIG_XT $CONFIG && return 89 90 #check if we don't need our internal header .. 91 cat >$TMPDIR/ipttest.c <<EOF 92#include <xtables.h> 93char *lib_dir; 94unsigned int global_option_offset = 0; 95const char *program_version = XTABLES_VERSION; 96const char *program_name = "tc-ipt"; 97struct afinfo afinfo = { 98 .libprefix = "libxt_", 99}; 100 101void exit_error(enum exittype status, const char *msg, ...) 102{ 103} 104 105int main(int argc, char **argv) { 106 107 return 0; 108} 109 110EOF 111 112 if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL -ldl >/dev/null 2>&1; then 113 echo "TC_CONFIG_XT_OLD:=y" >>$CONFIG 114 echo "using old xtables (no need for xt-internal.h)" 115 fi 116 rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest 117} 118 119check_xt_old_internal_h() 120{ 121 # bail if previous XT checks has already succeeded. 122 grep -q TC_CONFIG_XT $CONFIG && return 123 124 #check if we need our own internal.h 125 cat >$TMPDIR/ipttest.c <<EOF 126#include <xtables.h> 127#include "xt-internal.h" 128char *lib_dir; 129unsigned int global_option_offset = 0; 130const char *program_version = XTABLES_VERSION; 131const char *program_name = "tc-ipt"; 132struct afinfo afinfo = { 133 .libprefix = "libxt_", 134}; 135 136void exit_error(enum exittype status, const char *msg, ...) 137{ 138} 139 140int main(int argc, char **argv) { 141 142 return 0; 143} 144 145EOF 146 if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL -ldl >/dev/null 2>&1; then 147 echo "using old xtables with xt-internal.h" 148 echo "TC_CONFIG_XT_OLD_H:=y" >>$CONFIG 149 fi 150 rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest 151} 152 153check_lib_dir() 154{ 155 LIBDIR=$(echo $LIBDIR | sed "s|\${prefix}|$PREFIX|") 156 157 echo -n "lib directory: " 158 echo "$LIBDIR" 159 echo "LIBDIR:=$LIBDIR" >> $CONFIG 160} 161 162check_ipt() 163{ 164 if ! grep TC_CONFIG_XT $CONFIG > /dev/null; then 165 echo "using iptables" 166 fi 167} 168 169check_ipt_lib_dir() 170{ 171 IPT_LIB_DIR=$(${PKG_CONFIG} --variable=xtlibdir xtables) 172 if [ -n "$IPT_LIB_DIR" ]; then 173 echo $IPT_LIB_DIR 174 echo "IPT_LIB_DIR:=$IPT_LIB_DIR" >> $CONFIG 175 return 176 fi 177 178 for dir in /lib /usr/lib /usr/local/lib; do 179 for file in "xtables" "iptables"; do 180 file="$dir/$file/lib*t_*so" 181 if [ -f $file ]; then 182 echo ${file%/*} 183 echo "IPT_LIB_DIR:=${file%/*}" >> $CONFIG 184 return 185 fi 186 done 187 done 188 echo "not found!" 189} 190 191check_setns() 192{ 193 cat >$TMPDIR/setnstest.c <<EOF 194#include <sched.h> 195int main(int argc, char **argv) 196{ 197 (void)setns(0,0); 198 return 0; 199} 200EOF 201 if $CC -I$INCLUDE -o $TMPDIR/setnstest $TMPDIR/setnstest.c >/dev/null 2>&1; then 202 echo "IP_CONFIG_SETNS:=y" >>$CONFIG 203 echo "yes" 204 echo "CFLAGS += -DHAVE_SETNS" >>$CONFIG 205 else 206 echo "no" 207 fi 208 rm -f $TMPDIR/setnstest.c $TMPDIR/setnstest 209} 210 211check_name_to_handle_at() 212{ 213 cat >$TMPDIR/name_to_handle_at_test.c <<EOF 214#define _GNU_SOURCE 215#include <sys/types.h> 216#include <sys/stat.h> 217#include <fcntl.h> 218int main(int argc, char **argv) 219{ 220 struct file_handle *fhp; 221 int mount_id, flags, dirfd; 222 char *pathname; 223 name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags); 224 return 0; 225} 226EOF 227 if $CC -I$INCLUDE -o $TMPDIR/name_to_handle_at_test $TMPDIR/name_to_handle_at_test.c >/dev/null 2>&1; then 228 echo "yes" 229 echo "CFLAGS += -DHAVE_HANDLE_AT" >>$CONFIG 230 else 231 echo "no" 232 fi 233 rm -f $TMPDIR/name_to_handle_at_test.c $TMPDIR/name_to_handle_at_test 234} 235 236check_ipset() 237{ 238 cat >$TMPDIR/ipsettest.c <<EOF 239#include <linux/netfilter/ipset/ip_set.h> 240#ifndef IP_SET_INVALID 241#define IPSET_DIM_MAX 3 242typedef unsigned short ip_set_id_t; 243#endif 244#include <linux/netfilter/xt_set.h> 245 246struct xt_set_info info; 247#if IPSET_PROTOCOL == 6 || IPSET_PROTOCOL == 7 248int main(void) 249{ 250 return IPSET_MAXNAMELEN; 251} 252#else 253#error unknown ipset version 254#endif 255EOF 256 257 if $CC -I$INCLUDE -o $TMPDIR/ipsettest $TMPDIR/ipsettest.c >/dev/null 2>&1; then 258 echo "TC_CONFIG_IPSET:=y" >>$CONFIG 259 echo "yes" 260 else 261 echo "no" 262 fi 263 rm -f $TMPDIR/ipsettest.c $TMPDIR/ipsettest 264} 265 266check_elf() 267{ 268 if ${PKG_CONFIG} libelf --exists; then 269 echo "HAVE_ELF:=y" >>$CONFIG 270 echo "yes" 271 272 echo 'CFLAGS += -DHAVE_ELF' `${PKG_CONFIG} libelf --cflags` >> $CONFIG 273 echo 'LDLIBS += ' `${PKG_CONFIG} libelf --libs` >>$CONFIG 274 else 275 echo "no" 276 fi 277} 278 279have_libbpf_basic() 280{ 281 cat >$TMPDIR/libbpf_test.c <<EOF 282#include <bpf/libbpf.h> 283int main(int argc, char **argv) { 284 bpf_program__set_autoload(NULL, false); 285 bpf_map__ifindex(NULL); 286 bpf_map__set_pin_path(NULL, NULL); 287 bpf_object__open_file(NULL, NULL); 288 return 0; 289} 290EOF 291 292 $CC -o $TMPDIR/libbpf_test $TMPDIR/libbpf_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1 293 local ret=$? 294 295 rm -f $TMPDIR/libbpf_test.c $TMPDIR/libbpf_test 296 return $ret 297} 298 299have_libbpf_sec_name() 300{ 301 cat >$TMPDIR/libbpf_sec_test.c <<EOF 302#include <bpf/libbpf.h> 303int main(int argc, char **argv) { 304 void *ptr; 305 bpf_program__section_name(NULL); 306 return 0; 307} 308EOF 309 310 $CC -o $TMPDIR/libbpf_sec_test $TMPDIR/libbpf_sec_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1 311 local ret=$? 312 313 rm -f $TMPDIR/libbpf_sec_test.c $TMPDIR/libbpf_sec_test 314 return $ret 315} 316 317check_force_libbpf_on() 318{ 319 # if set LIBBPF_FORCE=on but no libbpf support, just exist the config 320 # process to make sure we don't build without libbpf. 321 if [ "$LIBBPF_FORCE" = on ]; then 322 echo " LIBBPF_FORCE=on set, but couldn't find a usable libbpf" 323 exit 1 324 fi 325} 326 327check_libbpf() 328{ 329 # if set LIBBPF_FORCE=off, disable libbpf entirely 330 if [ "$LIBBPF_FORCE" = off ]; then 331 echo "no" 332 return 333 fi 334 335 if ! ${PKG_CONFIG} libbpf --exists && [ -z "$LIBBPF_DIR" ] ; then 336 echo "no" 337 check_force_libbpf_on 338 return 339 fi 340 341 if [ $(uname -m) = x86_64 ]; then 342 local LIBBPF_LIBDIR="${LIBBPF_DIR}/usr/lib64" 343 else 344 local LIBBPF_LIBDIR="${LIBBPF_DIR}/usr/lib" 345 fi 346 347 if [ -n "$LIBBPF_DIR" ]; then 348 LIBBPF_CFLAGS="-I${LIBBPF_DIR}/usr/include" 349 LIBBPF_LDLIBS="${LIBBPF_LIBDIR}/libbpf.a -lz -lelf" 350 LIBBPF_VERSION=$(PKG_CONFIG_LIBDIR=${LIBBPF_LIBDIR}/pkgconfig ${PKG_CONFIG} libbpf --modversion) 351 else 352 LIBBPF_CFLAGS=$(${PKG_CONFIG} libbpf --cflags) 353 LIBBPF_LDLIBS=$(${PKG_CONFIG} libbpf --libs) 354 LIBBPF_VERSION=$(${PKG_CONFIG} libbpf --modversion) 355 fi 356 357 if ! have_libbpf_basic; then 358 echo "no" 359 echo " libbpf version $LIBBPF_VERSION is too low, please update it to at least 0.1.0" 360 check_force_libbpf_on 361 return 362 else 363 echo "HAVE_LIBBPF:=y" >> $CONFIG 364 echo 'CFLAGS += -DHAVE_LIBBPF ' $LIBBPF_CFLAGS >> $CONFIG 365 echo "CFLAGS += -DLIBBPF_VERSION=\\\"$LIBBPF_VERSION\\\"" >> $CONFIG 366 echo 'LDLIBS += ' $LIBBPF_LDLIBS >> $CONFIG 367 368 if [ -z "$LIBBPF_DIR" ]; then 369 echo "CFLAGS += -DLIBBPF_DYNAMIC" >> $CONFIG 370 fi 371 fi 372 373 # bpf_program__title() is deprecated since libbpf 0.2.0, use 374 # bpf_program__section_name() instead if we support 375 if have_libbpf_sec_name; then 376 echo "HAVE_LIBBPF_SECTION_NAME:=y" >> $CONFIG 377 echo 'CFLAGS += -DHAVE_LIBBPF_SECTION_NAME ' >> $CONFIG 378 fi 379 380 echo "yes" 381 echo " libbpf version $LIBBPF_VERSION" 382} 383 384check_selinux() 385# SELinux is a compile time option in the ss utility 386{ 387 if ${PKG_CONFIG} libselinux --exists; then 388 echo "HAVE_SELINUX:=y" >>$CONFIG 389 echo "yes" 390 391 echo 'LDLIBS +=' `${PKG_CONFIG} --libs libselinux` >>$CONFIG 392 echo 'CFLAGS += -DHAVE_SELINUX' `${PKG_CONFIG} --cflags libselinux` >>$CONFIG 393 else 394 echo "no" 395 fi 396} 397 398check_mnl() 399{ 400 if ${PKG_CONFIG} libmnl --exists; then 401 echo "HAVE_MNL:=y" >>$CONFIG 402 echo "yes" 403 404 echo 'CFLAGS += -DHAVE_LIBMNL' `${PKG_CONFIG} libmnl --cflags` >>$CONFIG 405 echo 'LDLIBS +=' `${PKG_CONFIG} libmnl --libs` >> $CONFIG 406 else 407 echo "no" 408 fi 409} 410 411check_berkeley_db() 412{ 413 cat >$TMPDIR/dbtest.c <<EOF 414#include <fcntl.h> 415#include <stdlib.h> 416#include <db_185.h> 417int main(int argc, char **argv) { 418 dbopen("/tmp/xxx_test_db.db", O_CREAT|O_RDWR, 0644, DB_HASH, NULL); 419 return 0; 420} 421EOF 422 if $CC -I$INCLUDE -o $TMPDIR/dbtest $TMPDIR/dbtest.c -ldb >/dev/null 2>&1; then 423 echo "HAVE_BERKELEY_DB:=y" >>$CONFIG 424 echo "yes" 425 else 426 echo "no" 427 fi 428 rm -f $TMPDIR/dbtest.c $TMPDIR/dbtest 429} 430 431check_strlcpy() 432{ 433 cat >$TMPDIR/strtest.c <<EOF 434#include <string.h> 435int main(int argc, char **argv) { 436 char dst[10]; 437 strlcpy(dst, "test", sizeof(dst)); 438 return 0; 439} 440EOF 441 if $CC -I$INCLUDE -o $TMPDIR/strtest $TMPDIR/strtest.c >/dev/null 2>&1; then 442 echo "no" 443 else 444 if ${PKG_CONFIG} libbsd --exists; then 445 echo 'CFLAGS += -DHAVE_LIBBSD' `${PKG_CONFIG} libbsd --cflags` >>$CONFIG 446 echo 'LDLIBS +=' `${PKG_CONFIG} libbsd --libs` >> $CONFIG 447 echo "no" 448 else 449 echo 'CFLAGS += -DNEED_STRLCPY' >>$CONFIG 450 echo "yes" 451 fi 452 fi 453 rm -f $TMPDIR/strtest.c $TMPDIR/strtest 454} 455 456check_cap() 457{ 458 if ${PKG_CONFIG} libcap --exists; then 459 echo "HAVE_CAP:=y" >>$CONFIG 460 echo "yes" 461 462 echo 'CFLAGS += -DHAVE_LIBCAP' `${PKG_CONFIG} libcap --cflags` >>$CONFIG 463 echo 'LDLIBS +=' `${PKG_CONFIG} libcap --libs` >> $CONFIG 464 else 465 echo "no" 466 fi 467} 468 469quiet_config() 470{ 471 cat <<EOF 472# user can control verbosity similar to kernel builds (e.g., V=1) 473ifeq ("\$(origin V)", "command line") 474 VERBOSE = \$(V) 475endif 476ifndef VERBOSE 477 VERBOSE = 0 478endif 479ifeq (\$(VERBOSE),1) 480 Q = 481else 482 Q = @ 483endif 484 485ifeq (\$(VERBOSE), 0) 486 QUIET_CC = @echo ' CC '\$@; 487 QUIET_AR = @echo ' AR '\$@; 488 QUIET_LINK = @echo ' LINK '\$@; 489 QUIET_YACC = @echo ' YACC '\$@; 490 QUIET_LEX = @echo ' LEX '\$@; 491endif 492EOF 493} 494 495usage() 496{ 497 cat <<EOF 498Usage: $0 [OPTIONS] 499 --include_dir <dir> Path to iproute2 include dir 500 --libdir <dir> Path to iproute2 lib dir 501 --libbpf_dir <dir> Path to libbpf DESTDIR 502 --libbpf_force <on|off> Enable/disable libbpf by force. Available options: 503 on: require link against libbpf, quit config if no libbpf support 504 off: disable libbpf probing 505 --prefix <dir> Path prefix of the lib files to install 506 -h | --help Show this usage info 507EOF 508 exit $1 509} 510 511# Compat with the old INCLUDE path setting method. 512if [ $# -eq 1 ] && [ "$(echo $1 | cut -c 1)" != '-' ]; then 513 INCLUDE="$1" 514else 515 while [ "$#" -gt 0 ]; do 516 case "$1" in 517 --include_dir) 518 shift 519 INCLUDE="$1" ;; 520 --include_dir=*) 521 INCLUDE="${1#*=}" ;; 522 --libdir) 523 shift 524 LIBDIR="$1" ;; 525 --libdir=*) 526 LIBDIR="${1#*=}" ;; 527 --libbpf_dir) 528 shift 529 LIBBPF_DIR="$1" ;; 530 --libbpf_dir=*) 531 LIBBPF_DIR="${1#*=}" ;; 532 --libbpf_force) 533 shift 534 LIBBPF_FORCE="$1" ;; 535 --libbpf_force=*) 536 LIBBPF_FORCE="${1#*=}" ;; 537 --prefix) 538 shift 539 PREFIX="$1" ;; 540 --prefix=*) 541 PREFIX="${1#*=}" ;; 542 -h | --help) 543 usage 0 ;; 544 --*) 545 ;; 546 *) 547 usage 1 ;; 548 esac 549 [ "$#" -gt 0 ] && shift 550 done 551fi 552 553[ -d "$INCLUDE" ] || usage 1 554if [ "${LIBBPF_DIR-unused}" != "unused" ]; then 555 [ -d "$LIBBPF_DIR" ] || usage 1 556fi 557if [ "${LIBBPF_FORCE-unused}" != "unused" ]; then 558 if [ "$LIBBPF_FORCE" != 'on' ] && [ "$LIBBPF_FORCE" != 'off' ]; then 559 usage 1 560 fi 561fi 562[ -z "$PREFIX" ] && usage 1 563[ -z "$LIBDIR" ] && usage 1 564 565echo "# Generated config based on" $INCLUDE >$CONFIG 566quiet_config >> $CONFIG 567 568check_toolchain 569 570echo "TC schedulers" 571 572echo -n " ATM " 573check_atm 574 575check_xtables 576if ! grep -q TC_CONFIG_NO_XT $CONFIG; then 577 echo -n " IPT " 578 check_xt 579 check_xt_old 580 check_xt_old_internal_h 581 check_ipt 582 583 echo -n " IPSET " 584 check_ipset 585fi 586 587echo 588check_lib_dir 589if ! grep -q TC_CONFIG_NO_XT $CONFIG; then 590 echo -n "iptables modules directory: " 591 check_ipt_lib_dir 592fi 593 594echo -n "libc has setns: " 595check_setns 596 597echo -n "libc has name_to_handle_at: " 598check_name_to_handle_at 599 600echo -n "SELinux support: " 601check_selinux 602 603echo -n "libbpf support: " 604check_libbpf 605 606echo -n "ELF support: " 607check_elf 608 609echo -n "libmnl support: " 610check_mnl 611 612echo -n "Berkeley DB: " 613check_berkeley_db 614 615echo -n "need for strlcpy: " 616check_strlcpy 617 618echo -n "libcap support: " 619check_cap 620 621echo >> $CONFIG 622echo "%.o: %.c" >> $CONFIG 623echo ' $(QUIET_CC)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CPPFLAGS) -c -o $@ $<' >> $CONFIG 624