1 /* SPDX-License-Identifier: GPL-2.0-or-later 2 * Copyright (c) 2021 Linux Test Project 3 */ 4 5 #ifndef TST_NETDEVICE_H 6 #define TST_NETDEVICE_H 7 8 /* Find device index for given network interface name. */ 9 int tst_netdev_index_by_name(const char *file, const int lineno, 10 const char *ifname); 11 #define NETDEV_INDEX_BY_NAME(ifname) \ 12 tst_netdev_index_by_name(__FILE__, __LINE__, (ifname)) 13 14 /* Activate or deactivate network interface */ 15 int tst_netdev_set_state(const char *file, const int lineno, 16 const char *ifname, int up); 17 #define NETDEV_SET_STATE(ifname, up) \ 18 tst_netdev_set_state(__FILE__, __LINE__, (ifname), (up)) 19 20 /* Create a connected pair of virtual network devices */ 21 int tst_create_veth_pair(const char *file, const int lineno, 22 const char *ifname1, const char *ifname2); 23 #define CREATE_VETH_PAIR(ifname1, ifname2) \ 24 tst_create_veth_pair(__FILE__, __LINE__, (ifname1), (ifname2)) 25 26 int tst_netdev_add_device(const char *file, const int lineno, 27 const char *ifname, const char *devtype); 28 #define NETDEV_ADD_DEVICE(ifname, devtype) \ 29 tst_netdev_add_device(__FILE__, __LINE__, (ifname), (devtype)) 30 31 int tst_netdev_remove_device(const char *file, const int lineno, 32 const char *ifname); 33 #define NETDEV_REMOVE_DEVICE(ifname) \ 34 tst_netdev_remove_device(__FILE__, __LINE__, (ifname)) 35 36 int tst_netdev_add_address(const char *file, const int lineno, 37 const char *ifname, unsigned int family, const void *address, 38 unsigned int prefix, size_t addrlen, unsigned int flags); 39 #define NETDEV_ADD_ADDRESS(ifname, family, address, prefix, addrlen, flags) \ 40 tst_netdev_add_address(__FILE__, __LINE__, (ifname), (family), \ 41 (address), (prefix), (addrlen), (flags)) 42 43 int tst_netdev_add_address_inet(const char *file, const int lineno, 44 const char *ifname, in_addr_t address, unsigned int prefix, 45 unsigned int flags); 46 #define NETDEV_ADD_ADDRESS_INET(ifname, address, prefix, flags) \ 47 tst_netdev_add_address_inet(__FILE__, __LINE__, (ifname), (address), \ 48 (prefix), (flags)) 49 50 int tst_netdev_remove_address(const char *file, const int lineno, 51 const char *ifname, unsigned int family, const void *address, 52 size_t addrlen); 53 #define NETDEV_REMOVE_ADDRESS(ifname, family, address, addrlen) \ 54 tst_netdev_remove_address(__FILE__, __LINE__, (ifname), (family), \ 55 (address), (addrlen)) 56 57 int tst_netdev_remove_address_inet(const char *file, const int lineno, 58 const char *ifname, in_addr_t address); 59 #define NETDEV_REMOVE_ADDRESS_INET(ifname, address) \ 60 tst_netdev_remove_address_inet(__FILE__, __LINE__, (ifname), (address)) 61 62 int tst_netdev_change_ns_fd(const char *file, const int lineno, 63 const char *ifname, int nsfd); 64 #define NETDEV_CHANGE_NS_FD(ifname, nsfd) \ 65 tst_netdev_change_ns_fd(__FILE__, __LINE__, (ifname), (nsfd)) 66 67 int tst_netdev_change_ns_pid(const char *file, const int lineno, 68 const char *ifname, pid_t nspid); 69 #define NETDEV_CHANGE_NS_PID(ifname, nspid) \ 70 tst_netdev_change_ns_pid(__FILE__, __LINE__, (ifname), (nspid)) 71 72 /* 73 * Add new static entry to main routing table. If you specify gateway address, 74 * the interface name is optional. 75 */ 76 int tst_netdev_add_route(const char *file, const int lineno, 77 const char *ifname, unsigned int family, const void *srcaddr, 78 unsigned int srcprefix, size_t srclen, const void *dstaddr, 79 unsigned int dstprefix, size_t dstlen, const void *gateway, 80 size_t gatewaylen); 81 #define NETDEV_ADD_ROUTE(ifname, family, srcaddr, srcprefix, srclen, dstaddr, \ 82 dstprefix, dstlen, gateway, gatewaylen) \ 83 tst_netdev_add_route(__FILE__, __LINE__, (ifname), (family), \ 84 (srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \ 85 (dstlen), (gateway), (gatewaylen)) 86 87 /* 88 * Simplified function for adding IPv4 static route. If you set srcprefix 89 * or dstprefix to 0, the corresponding address will be ignored. Interface 90 * name is optional if gateway address is non-zero. 91 */ 92 int tst_netdev_add_route_inet(const char *file, const int lineno, 93 const char *ifname, in_addr_t srcaddr, unsigned int srcprefix, 94 in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway); 95 #define NETDEV_ADD_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, dstprefix, \ 96 gateway) \ 97 tst_netdev_add_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \ 98 (srcprefix), (dstaddr), (dstprefix), (gateway)) 99 100 /* 101 * Remove static entry from main routing table. 102 */ 103 int tst_netdev_remove_route(const char *file, const int lineno, 104 const char *ifname, unsigned int family, const void *srcaddr, 105 unsigned int srcprefix, size_t srclen, const void *dstaddr, 106 unsigned int dstprefix, size_t dstlen, const void *gateway, 107 size_t gatewaylen); 108 #define NETDEV_REMOVE_ROUTE(ifname, family, srcaddr, srcprefix, srclen, \ 109 dstaddr, dstprefix, dstlen, gateway, gatewaylen) \ 110 tst_netdev_remove_route(__FILE__, __LINE__, (ifname), (family), \ 111 (srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \ 112 (dstlen), (gateway), (gatewaylen)) 113 114 /* 115 * Simplified function for removing IPv4 static route. 116 */ 117 int tst_netdev_remove_route_inet(const char *file, const int lineno, 118 const char *ifname, in_addr_t srcaddr, unsigned int srcprefix, 119 in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway); 120 #define NETDEV_REMOVE_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, \ 121 dstprefix, gateway) \ 122 tst_netdev_remove_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \ 123 (srcprefix), (dstaddr), (dstprefix), (gateway)) 124 125 #endif /* TST_NETDEVICE_H */ 126