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