1*49cdfc7eSAndroid Build Coastguard Worker /* SPDX-License-Identifier: GPL-2.0-or-later 2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2021 Linux Test Project 3*49cdfc7eSAndroid Build Coastguard Worker */ 4*49cdfc7eSAndroid Build Coastguard Worker 5*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_NETDEVICE_H 6*49cdfc7eSAndroid Build Coastguard Worker #define TST_NETDEVICE_H 7*49cdfc7eSAndroid Build Coastguard Worker 8*49cdfc7eSAndroid Build Coastguard Worker #include "tst_netlink.h" 9*49cdfc7eSAndroid Build Coastguard Worker 10*49cdfc7eSAndroid Build Coastguard Worker /* Find device index for given network interface name. */ 11*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_index_by_name(const char *file, const int lineno, 12*49cdfc7eSAndroid Build Coastguard Worker const char *ifname); 13*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_INDEX_BY_NAME(ifname) \ 14*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_index_by_name(__FILE__, __LINE__, (ifname)) 15*49cdfc7eSAndroid Build Coastguard Worker 16*49cdfc7eSAndroid Build Coastguard Worker /* Activate or deactivate network interface */ 17*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_set_state(const char *file, const int lineno, 18*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, int up); 19*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_SET_STATE(ifname, up) \ 20*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_set_state(__FILE__, __LINE__, (ifname), (up)) 21*49cdfc7eSAndroid Build Coastguard Worker 22*49cdfc7eSAndroid Build Coastguard Worker /* Create a connected pair of virtual network devices */ 23*49cdfc7eSAndroid Build Coastguard Worker int tst_create_veth_pair(const char *file, const int lineno, int strict, 24*49cdfc7eSAndroid Build Coastguard Worker const char *ifname1, const char *ifname2); 25*49cdfc7eSAndroid Build Coastguard Worker #define CREATE_VETH_PAIR(ifname1, ifname2) \ 26*49cdfc7eSAndroid Build Coastguard Worker tst_create_veth_pair(__FILE__, __LINE__, 1, (ifname1), (ifname2)) 27*49cdfc7eSAndroid Build Coastguard Worker 28*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_device(const char *file, const int lineno, int strict, 29*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, const char *devtype); 30*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_DEVICE(ifname, devtype) \ 31*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_device(__FILE__, __LINE__, 1, (ifname), (devtype)) 32*49cdfc7eSAndroid Build Coastguard Worker 33*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_device(const char *file, const int lineno, int strict, 34*49cdfc7eSAndroid Build Coastguard Worker const char *ifname); 35*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_DEVICE(ifname) \ 36*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_device(__FILE__, __LINE__, 1, (ifname)) 37*49cdfc7eSAndroid Build Coastguard Worker 38*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_address(const char *file, const int lineno, int strict, 39*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, unsigned int family, const void *address, 40*49cdfc7eSAndroid Build Coastguard Worker unsigned int prefix, size_t addrlen, unsigned int flags); 41*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_ADDRESS(ifname, family, address, prefix, addrlen, flags) \ 42*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_address(__FILE__, __LINE__, 1, (ifname), (family), \ 43*49cdfc7eSAndroid Build Coastguard Worker (address), (prefix), (addrlen), (flags)) 44*49cdfc7eSAndroid Build Coastguard Worker 45*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_address_inet(const char *file, const int lineno, int strict, 46*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, in_addr_t address, unsigned int prefix, 47*49cdfc7eSAndroid Build Coastguard Worker unsigned int flags); 48*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_ADDRESS_INET(ifname, address, prefix, flags) \ 49*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_address_inet(__FILE__, __LINE__, 1, (ifname), \ 50*49cdfc7eSAndroid Build Coastguard Worker (address), (prefix), (flags)) 51*49cdfc7eSAndroid Build Coastguard Worker 52*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_address(const char *file, const int lineno, int strict, 53*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, unsigned int family, const void *address, 54*49cdfc7eSAndroid Build Coastguard Worker size_t addrlen); 55*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_ADDRESS(ifname, family, address, addrlen) \ 56*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_address(__FILE__, __LINE__, 1, (ifname), (family), \ 57*49cdfc7eSAndroid Build Coastguard Worker (address), (addrlen)) 58*49cdfc7eSAndroid Build Coastguard Worker 59*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_address_inet(const char *file, const int lineno, 60*49cdfc7eSAndroid Build Coastguard Worker int strict, const char *ifname, in_addr_t address); 61*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_ADDRESS_INET(ifname, address) \ 62*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_address_inet(__FILE__, __LINE__, 1, (ifname), \ 63*49cdfc7eSAndroid Build Coastguard Worker (address)) 64*49cdfc7eSAndroid Build Coastguard Worker 65*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_change_ns_fd(const char *file, const int lineno, int strict, 66*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, int nsfd); 67*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_CHANGE_NS_FD(ifname, nsfd) \ 68*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_change_ns_fd(__FILE__, __LINE__, 1, (ifname), (nsfd)) 69*49cdfc7eSAndroid Build Coastguard Worker 70*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_change_ns_pid(const char *file, const int lineno, int strict, 71*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, pid_t nspid); 72*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_CHANGE_NS_PID(ifname, nspid) \ 73*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_change_ns_pid(__FILE__, __LINE__, 1, (ifname), (nspid)) 74*49cdfc7eSAndroid Build Coastguard Worker 75*49cdfc7eSAndroid Build Coastguard Worker /* 76*49cdfc7eSAndroid Build Coastguard Worker * Add new static entry to main routing table. If you specify gateway address, 77*49cdfc7eSAndroid Build Coastguard Worker * the interface name is optional. 78*49cdfc7eSAndroid Build Coastguard Worker */ 79*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_route(const char *file, const int lineno, int strict, 80*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, unsigned int family, const void *srcaddr, 81*49cdfc7eSAndroid Build Coastguard Worker unsigned int srcprefix, size_t srclen, const void *dstaddr, 82*49cdfc7eSAndroid Build Coastguard Worker unsigned int dstprefix, size_t dstlen, const void *gateway, 83*49cdfc7eSAndroid Build Coastguard Worker size_t gatewaylen); 84*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_ROUTE(ifname, family, srcaddr, srcprefix, srclen, dstaddr, \ 85*49cdfc7eSAndroid Build Coastguard Worker dstprefix, dstlen, gateway, gatewaylen) \ 86*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_route(__FILE__, __LINE__, 1, (ifname), (family), \ 87*49cdfc7eSAndroid Build Coastguard Worker (srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \ 88*49cdfc7eSAndroid Build Coastguard Worker (dstlen), (gateway), (gatewaylen)) 89*49cdfc7eSAndroid Build Coastguard Worker 90*49cdfc7eSAndroid Build Coastguard Worker /* 91*49cdfc7eSAndroid Build Coastguard Worker * Simplified function for adding IPv4 static route. If you set srcprefix 92*49cdfc7eSAndroid Build Coastguard Worker * or dstprefix to 0, the corresponding address will be ignored. Interface 93*49cdfc7eSAndroid Build Coastguard Worker * name is optional if gateway address is non-zero. 94*49cdfc7eSAndroid Build Coastguard Worker */ 95*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_route_inet(const char *file, const int lineno, int strict, 96*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, in_addr_t srcaddr, unsigned int srcprefix, 97*49cdfc7eSAndroid Build Coastguard Worker in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway); 98*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, dstprefix, \ 99*49cdfc7eSAndroid Build Coastguard Worker gateway) \ 100*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_route_inet(__FILE__, __LINE__, 1, (ifname), (srcaddr), \ 101*49cdfc7eSAndroid Build Coastguard Worker (srcprefix), (dstaddr), (dstprefix), (gateway)) 102*49cdfc7eSAndroid Build Coastguard Worker 103*49cdfc7eSAndroid Build Coastguard Worker /* 104*49cdfc7eSAndroid Build Coastguard Worker * Remove static entry from main routing table. 105*49cdfc7eSAndroid Build Coastguard Worker */ 106*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_route(const char *file, const int lineno, int strict, 107*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, unsigned int family, const void *srcaddr, 108*49cdfc7eSAndroid Build Coastguard Worker unsigned int srcprefix, size_t srclen, const void *dstaddr, 109*49cdfc7eSAndroid Build Coastguard Worker unsigned int dstprefix, size_t dstlen, const void *gateway, 110*49cdfc7eSAndroid Build Coastguard Worker size_t gatewaylen); 111*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_ROUTE(ifname, family, srcaddr, srcprefix, srclen, \ 112*49cdfc7eSAndroid Build Coastguard Worker dstaddr, dstprefix, dstlen, gateway, gatewaylen) \ 113*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_route(__FILE__, __LINE__, 1, (ifname), (family), \ 114*49cdfc7eSAndroid Build Coastguard Worker (srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \ 115*49cdfc7eSAndroid Build Coastguard Worker (dstlen), (gateway), (gatewaylen)) 116*49cdfc7eSAndroid Build Coastguard Worker 117*49cdfc7eSAndroid Build Coastguard Worker /* 118*49cdfc7eSAndroid Build Coastguard Worker * Simplified function for removing IPv4 static route. 119*49cdfc7eSAndroid Build Coastguard Worker */ 120*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_route_inet(const char *file, const int lineno, 121*49cdfc7eSAndroid Build Coastguard Worker int strict, const char *ifname, in_addr_t srcaddr, 122*49cdfc7eSAndroid Build Coastguard Worker unsigned int srcprefix, in_addr_t dstaddr, unsigned int dstprefix, 123*49cdfc7eSAndroid Build Coastguard Worker in_addr_t gateway); 124*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, \ 125*49cdfc7eSAndroid Build Coastguard Worker dstprefix, gateway) \ 126*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_route_inet(__FILE__, __LINE__, 1, (ifname), \ 127*49cdfc7eSAndroid Build Coastguard Worker (srcaddr), (srcprefix), (dstaddr), (dstprefix), (gateway)) 128*49cdfc7eSAndroid Build Coastguard Worker 129*49cdfc7eSAndroid Build Coastguard Worker /* 130*49cdfc7eSAndroid Build Coastguard Worker * Add queueing discipline. Network interface name is optional. 131*49cdfc7eSAndroid Build Coastguard Worker */ 132*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_qdisc(const char *file, const int lineno, int strict, 133*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, unsigned int family, unsigned int parent, 134*49cdfc7eSAndroid Build Coastguard Worker unsigned int handle, const char *qd_kind, 135*49cdfc7eSAndroid Build Coastguard Worker const struct tst_netlink_attr_list *config); 136*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_QDISC(ifname, family, parent, handle, qd_kind, config) \ 137*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_qdisc(__FILE__, __LINE__, 1, (ifname), (family), \ 138*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (qd_kind), (config)) 139*49cdfc7eSAndroid Build Coastguard Worker 140*49cdfc7eSAndroid Build Coastguard Worker /* 141*49cdfc7eSAndroid Build Coastguard Worker * Remove queueing discipline. 142*49cdfc7eSAndroid Build Coastguard Worker */ 143*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_qdisc(const char *file, const int lineno, int strict, 144*49cdfc7eSAndroid Build Coastguard Worker const char *ifname, unsigned int family, unsigned int parent, 145*49cdfc7eSAndroid Build Coastguard Worker unsigned int handle, const char *qd_kind); 146*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_QDISC(ifname, family, parent, handle, qd_kind) \ 147*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_qdisc(__FILE__, __LINE__, 1, (ifname), (family), \ 148*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (qd_kind)) 149*49cdfc7eSAndroid Build Coastguard Worker 150*49cdfc7eSAndroid Build Coastguard Worker /* 151*49cdfc7eSAndroid Build Coastguard Worker * Add traffic class to queueing discipline. Network interface name is 152*49cdfc7eSAndroid Build Coastguard Worker * optional. 153*49cdfc7eSAndroid Build Coastguard Worker */ 154*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_traffic_class(const char *file, const int lineno, 155*49cdfc7eSAndroid Build Coastguard Worker int strict, const char *ifname, unsigned int parent, 156*49cdfc7eSAndroid Build Coastguard Worker unsigned int handle, const char *qd_kind, 157*49cdfc7eSAndroid Build Coastguard Worker const struct tst_netlink_attr_list *config); 158*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_TRAFFIC_CLASS(ifname, parent, handle, qd_kind, config) \ 159*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_traffic_class(__FILE__, __LINE__, 1, (ifname), \ 160*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (qd_kind), (config)) 161*49cdfc7eSAndroid Build Coastguard Worker 162*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_traffic_class(const char *file, const int lineno, 163*49cdfc7eSAndroid Build Coastguard Worker int strict, const char *ifname, unsigned int parent, 164*49cdfc7eSAndroid Build Coastguard Worker unsigned int handle, const char *qd_kind); 165*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_TRAFFIC_CLASS(ifname, parent, handle, qd_kind) \ 166*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_traffic_class(__FILE__, __LINE__, 1, (ifname), \ 167*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (qd_kind)) 168*49cdfc7eSAndroid Build Coastguard Worker 169*49cdfc7eSAndroid Build Coastguard Worker /* 170*49cdfc7eSAndroid Build Coastguard Worker * Add traffic filter to queueing discipline. Protocol should be en ETH_P_* 171*49cdfc7eSAndroid Build Coastguard Worker * constant in host byte order. Network interface name is optional. 172*49cdfc7eSAndroid Build Coastguard Worker */ 173*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_add_traffic_filter(const char *file, const int lineno, 174*49cdfc7eSAndroid Build Coastguard Worker int strict, const char *ifname, unsigned int parent, 175*49cdfc7eSAndroid Build Coastguard Worker unsigned int handle, unsigned int protocol, unsigned int priority, 176*49cdfc7eSAndroid Build Coastguard Worker const char *f_kind, const struct tst_netlink_attr_list *config); 177*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_TRAFFIC_FILTER(ifname, parent, handle, protocol, priority, \ 178*49cdfc7eSAndroid Build Coastguard Worker f_kind, config) \ 179*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_traffic_filter(__FILE__, __LINE__, 1, (ifname), \ 180*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (protocol), (priority), (f_kind), (config)) 181*49cdfc7eSAndroid Build Coastguard Worker 182*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_ADD_TRAFFIC_FILTER_RET(ifname, parent, handle, protocol, \ 183*49cdfc7eSAndroid Build Coastguard Worker priority, f_kind, config) \ 184*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_add_traffic_filter(__FILE__, __LINE__, 0, (ifname), \ 185*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (protocol), (priority), (f_kind), (config)) 186*49cdfc7eSAndroid Build Coastguard Worker 187*49cdfc7eSAndroid Build Coastguard Worker int tst_netdev_remove_traffic_filter(const char *file, const int lineno, 188*49cdfc7eSAndroid Build Coastguard Worker int strict, const char *ifname, unsigned int parent, 189*49cdfc7eSAndroid Build Coastguard Worker unsigned int handle, unsigned int protocol, unsigned int priority, 190*49cdfc7eSAndroid Build Coastguard Worker const char *f_kind); 191*49cdfc7eSAndroid Build Coastguard Worker #define NETDEV_REMOVE_TRAFFIC_FILTER(ifname, parent, handle, protocol, \ 192*49cdfc7eSAndroid Build Coastguard Worker priority, f_kind) \ 193*49cdfc7eSAndroid Build Coastguard Worker tst_netdev_remove_traffic_filter(__FILE__, __LINE__, 1, (ifname), \ 194*49cdfc7eSAndroid Build Coastguard Worker (parent), (handle), (protocol), (priority), (f_kind)) 195*49cdfc7eSAndroid Build Coastguard Worker 196*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_NETDEVICE_H */ 197