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