• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_remove_netdev(const char *file, const int lineno, const char *ifname);
27 #define REMOVE_NETDEV(ifname) tst_remove_netdev(__FILE__, __LINE__, (ifname))
28 
29 int tst_netdev_add_address(const char *file, const int lineno,
30 	const char *ifname, unsigned int family, const void *address,
31 	unsigned int prefix, size_t addrlen, unsigned int flags);
32 #define NETDEV_ADD_ADDRESS(ifname, family, address, prefix, addrlen, flags) \
33 	tst_netdev_add_address(__FILE__, __LINE__, (ifname), (family), \
34 		(address), (prefix), (addrlen), (flags))
35 
36 int tst_netdev_add_address_inet(const char *file, const int lineno,
37 	const char *ifname, in_addr_t address, unsigned int prefix,
38 	unsigned int flags);
39 #define NETDEV_ADD_ADDRESS_INET(ifname, address, prefix, flags) \
40 	tst_netdev_add_address_inet(__FILE__, __LINE__, (ifname), (address), \
41 		(prefix), (flags))
42 
43 int tst_netdev_remove_address(const char *file, const int lineno,
44 	const char *ifname, unsigned int family, const void *address,
45 	size_t addrlen);
46 #define NETDEV_REMOVE_ADDRESS(ifname, family, address, addrlen) \
47 	tst_netdev_remove_address(__FILE__, __LINE__, (ifname), (family), \
48 		(address), (addrlen))
49 
50 int tst_netdev_remove_address_inet(const char *file, const int lineno,
51 	const char *ifname, in_addr_t address);
52 #define NETDEV_REMOVE_ADDRESS_INET(ifname, address) \
53 	tst_netdev_remove_address_inet(__FILE__, __LINE__, (ifname), (address))
54 
55 int tst_netdev_change_ns_fd(const char *file, const int lineno,
56 	const char *ifname, int nsfd);
57 #define NETDEV_CHANGE_NS_FD(ifname, nsfd) \
58 	tst_netdev_change_ns_fd(__FILE__, __LINE__, (ifname), (nsfd))
59 
60 int tst_netdev_change_ns_pid(const char *file, const int lineno,
61 	const char *ifname, pid_t nspid);
62 #define NETDEV_CHANGE_NS_PID(ifname, nspid) \
63 	tst_netdev_change_ns_pid(__FILE__, __LINE__, (ifname), (nspid))
64 
65 /*
66  * Add new static entry to main routing table. If you specify gateway address,
67  * the interface name is optional.
68  */
69 int tst_netdev_add_route(const char *file, const int lineno,
70 	const char *ifname, unsigned int family, const void *srcaddr,
71 	unsigned int srcprefix, size_t srclen, const void *dstaddr,
72 	unsigned int dstprefix, size_t dstlen, const void *gateway,
73 	size_t gatewaylen);
74 #define NETDEV_ADD_ROUTE(ifname, family, srcaddr, srcprefix, srclen, dstaddr, \
75 	dstprefix, dstlen, gateway, gatewaylen) \
76 	tst_netdev_add_route(__FILE__, __LINE__, (ifname), (family), \
77 		(srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \
78 		(dstlen), (gateway), (gatewaylen))
79 
80 /*
81  * Simplified function for adding IPv4 static route. If you set srcprefix
82  * or dstprefix to 0, the corresponding address will be ignored. Interface
83  * name is optional if gateway address is non-zero.
84  */
85 int tst_netdev_add_route_inet(const char *file, const int lineno,
86 	const char *ifname, in_addr_t srcaddr, unsigned int srcprefix,
87 	in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway);
88 #define NETDEV_ADD_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, dstprefix, \
89 	gateway) \
90 	tst_netdev_add_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \
91 		(srcprefix), (dstaddr), (dstprefix), (gateway))
92 
93 /*
94  * Remove static entry from main routing table.
95  */
96 int tst_netdev_remove_route(const char *file, const int lineno,
97 	const char *ifname, unsigned int family, const void *srcaddr,
98 	unsigned int srcprefix, size_t srclen, const void *dstaddr,
99 	unsigned int dstprefix, size_t dstlen, const void *gateway,
100 	size_t gatewaylen);
101 #define NETDEV_REMOVE_ROUTE(ifname, family, srcaddr, srcprefix, srclen, \
102 	dstaddr, dstprefix, dstlen, gateway, gatewaylen) \
103 	tst_netdev_remove_route(__FILE__, __LINE__, (ifname), (family), \
104 		(srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \
105 		(dstlen), (gateway), (gatewaylen))
106 
107 /*
108  * Simplified function for removing IPv4 static route.
109  */
110 int tst_netdev_remove_route_inet(const char *file, const int lineno,
111 	const char *ifname, in_addr_t srcaddr, unsigned int srcprefix,
112 	in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway);
113 #define NETDEV_REMOVE_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, \
114 	dstprefix, gateway) \
115 	tst_netdev_remove_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \
116 		(srcprefix), (dstaddr), (dstprefix), (gateway))
117 
118 #endif /* TST_NETDEVICE_H */
119