blob: f5493dd088ee6c8f7b51e2d597a30daf5077d9bf [file] [log] [blame]
Damjan Marion83243a02016-02-29 13:09:30 +01001/*
2 *------------------------------------------------------------------
3 * af_packet.c - linux kernel packet interface
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <linux/if_ether.h>
21#include <linux/if_packet.h>
Mohsin Kazmia50a14c2018-04-25 15:58:05 +020022#include <sys/ioctl.h>
23#include <net/if.h>
Ray Kinsellac855b732017-04-21 12:24:43 +010024#include <dirent.h>
Ray Kinsella7bfa1192017-05-15 11:52:43 +010025#include <sys/stat.h>
26#include <sys/types.h>
27#include <fcntl.h>
Damjan Marion83243a02016-02-29 13:09:30 +010028
Damjan Marion01914ce2017-09-14 19:04:50 +020029#include <vppinfra/linux/sysfs.h>
Damjan Marion83243a02016-02-29 13:09:30 +010030#include <vlib/vlib.h>
31#include <vlib/unix/unix.h>
32#include <vnet/ip/ip.h>
Aloys Augustine39376e2021-03-29 22:08:09 +020033#include <vnet/devices/netlink.h>
Damjan Marion83243a02016-02-29 13:09:30 +010034#include <vnet/ethernet/ethernet.h>
Mohammed Hawari85c19432020-12-18 16:29:45 +010035#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +000036#include <vnet/interface/tx_queue_funcs.h>
Damjan Marion83243a02016-02-29 13:09:30 +010037
38#include <vnet/devices/af_packet/af_packet.h>
39
Dave Wallace71612d62017-10-24 01:32:41 -040040af_packet_main_t af_packet_main;
41
Mohsin Kazmicae84fa2021-10-08 15:10:49 +000042VNET_HW_INTERFACE_CLASS (af_packet_ip_device_hw_interface_class, static) = {
43 .name = "af-packet-ip-device",
44 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
45};
46
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020047#define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +000048#define AF_PACKET_DEFAULT_TX_FRAME_SIZE (2048 * 33) // GSO packet of 64KB
Damjan Marion83243a02016-02-29 13:09:30 +010049#define AF_PACKET_TX_BLOCK_NR 1
Damjan Marion83243a02016-02-29 13:09:30 +010050
Mohsin Kazmi8b90d892022-09-08 17:21:20 +000051#define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK_V2 1024
52#define AF_PACKET_DEFAULT_RX_FRAME_SIZE_V2 (2048 * 33) // GSO packet of 64KB
53#define AF_PACKET_RX_BLOCK_NR_V2 1
54
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +000055#define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 32
Mohsin Kazmi219cbcb2022-03-18 16:58:31 +000056#define AF_PACKET_DEFAULT_RX_FRAME_SIZE 2048
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +000057#define AF_PACKET_RX_BLOCK_NR 160
Damjan Marion83243a02016-02-29 13:09:30 +010058
Damjan Marion83243a02016-02-29 13:09:30 +010059/*defined in net/if.h but clashes with dpdk headers */
Damjan Marion00a9dca2016-08-17 17:05:46 +020060unsigned int if_nametoindex (const char *ifname);
Damjan Marion83243a02016-02-29 13:09:30 +010061
Damjan Marion88a9c0e2022-01-06 21:14:08 +010062static clib_error_t *
Damjan Marion1cd0e5d2022-01-17 14:49:17 +010063af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
64 u32 frame_size)
Damjan Marion83243a02016-02-29 13:09:30 +010065{
Damjan Marion88a9c0e2022-01-06 21:14:08 +010066 clib_error_t *error, *rv;
Ray Kinsella7bfa1192017-05-15 11:52:43 +010067 af_packet_main_t *apm = &af_packet_main;
Damjan Marion88a9c0e2022-01-06 21:14:08 +010068 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);
Ray Kinsella7bfa1192017-05-15 11:52:43 +010069
Damjan Marion1cd0e5d2022-01-17 14:49:17 +010070 error = vnet_netlink_set_link_mtu (apif->host_if_index,
71 frame_size + hi->frame_overhead);
Damjan Marion88a9c0e2022-01-06 21:14:08 +010072
73 if (error)
Ray Kinsella7bfa1192017-05-15 11:52:43 +010074 {
Damjan Marion88a9c0e2022-01-06 21:14:08 +010075 vlib_log_err (apm->log_class, "netlink failed to change MTU: %U",
76 format_clib_error, error);
77 rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U",
78 format_clib_error, error);
79 clib_error_free (error);
80 return rv;
Ray Kinsella7bfa1192017-05-15 11:52:43 +010081 }
Damjan Marion88a9c0e2022-01-06 21:14:08 +010082 else
Damjan Marion1cd0e5d2022-01-17 14:49:17 +010083 apif->host_mtu = frame_size + hi->frame_overhead;
Damjan Marion83243a02016-02-29 13:09:30 +010084 return 0;
85}
86
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +010087static int
88af_packet_read_mtu (af_packet_if_t *apif)
89{
90 af_packet_main_t *apm = &af_packet_main;
91 clib_error_t *error;
Aloys Augustine39376e2021-03-29 22:08:09 +020092 error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +010093 if (error)
94 {
Aloys Augustine39376e2021-03-29 22:08:09 +020095 vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +010096 format_clib_error, error);
97 clib_error_free (error);
98 return VNET_API_ERROR_SYSCALL_ERROR_1;
99 }
100 return 0;
101}
102
Damjan Marion00a9dca2016-08-17 17:05:46 +0200103static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200104af_packet_fd_read_ready (clib_file_t * uf)
Damjan Marion83243a02016-02-29 13:09:30 +0100105{
Damjan Marioneb743fa2017-03-20 16:34:15 +0100106 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion83243a02016-02-29 13:09:30 +0100107
Damjan Marion83243a02016-02-29 13:09:30 +0100108 /* Schedule the rx node */
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000109 vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
Damjan Marion83243a02016-02-29 13:09:30 +0100110 return 0;
111}
112
113static int
Ray Kinsellac855b732017-04-21 12:24:43 +0100114is_bridge (const u8 * host_if_name)
115{
116 u8 *s;
117 DIR *dir = NULL;
118
119 s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
120 dir = opendir ((char *) s);
121 vec_free (s);
122
123 if (dir)
124 {
125 closedir (dir);
126 return 0;
127 }
128
129 return -1;
130}
131
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000132static void
133af_packet_set_rx_queues (vlib_main_t *vm, af_packet_if_t *apif)
134{
135 vnet_main_t *vnm = vnet_get_main ();
136 af_packet_queue_t *rx_queue;
137
138 vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
139 af_packet_input_node.index);
140
141 vec_foreach (rx_queue, apif->rx_queues)
142 {
143 rx_queue->queue_index = vnet_hw_if_register_rx_queue (
144 vnm, apif->hw_if_index, rx_queue->queue_id, VNET_HW_IF_RXQ_THREAD_ANY);
145
146 {
147 clib_file_t template = { 0 };
148 template.read_function = af_packet_fd_read_ready;
149 template.file_descriptor = rx_queue->fd;
150 template.private_data = rx_queue->queue_index;
151 template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
152 template.description =
153 format (0, "%U queue %u", format_af_packet_device_name,
154 apif->dev_instance, rx_queue->queue_id);
155 rx_queue->clib_file_index = clib_file_add (&file_main, &template);
156 }
157 vnet_hw_if_set_rx_queue_file_index (vnm, rx_queue->queue_index,
158 rx_queue->clib_file_index);
159 vnet_hw_if_set_rx_queue_mode (vnm, rx_queue->queue_index,
160 VNET_HW_IF_RX_MODE_INTERRUPT);
161 rx_queue->mode = VNET_HW_IF_RX_MODE_INTERRUPT;
162 }
163 vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
164}
165
166static void
167af_packet_set_tx_queues (vlib_main_t *vm, af_packet_if_t *apif)
168{
169 vnet_main_t *vnm = vnet_get_main ();
170 af_packet_main_t *apm = &af_packet_main;
171 af_packet_queue_t *tx_queue;
172
173 vec_foreach (tx_queue, apif->tx_queues)
174 {
175 tx_queue->queue_index = vnet_hw_if_register_tx_queue (
176 vnm, apif->hw_if_index, tx_queue->queue_id);
177 }
178
179 if (apif->num_txqs == 0)
180 {
181 vlib_log_err (apm->log_class, "Interface %U has 0 txq",
182 format_vnet_hw_if_index_name, vnm, apif->hw_if_index);
183 return;
184 }
185
186 for (u32 j = 0; j < vlib_get_n_threads (); j++)
187 {
188 u32 qi = apif->tx_queues[j % apif->num_txqs].queue_index;
189 vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
190 }
191
192 vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
193}
194
Ray Kinsellac855b732017-04-21 12:24:43 +0100195static int
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000196create_packet_sock (int host_if_index, tpacket_req_u_t *rx_req,
197 tpacket_req_u_t *tx_req, int *fd, af_packet_ring_t *ring,
198 u32 fanout_id, af_packet_if_flags_t *flags, int ver)
Damjan Marion83243a02016-02-29 13:09:30 +0100199{
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200200 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100201 struct sockaddr_ll sll;
Mohsin Kazmi219cbcb2022-03-18 16:58:31 +0000202 socklen_t req_sz = sizeof (tpacket_req3_t);
203 int ret;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000204 u32 ring_sz = 0;
205
Damjan Marion00a9dca2016-08-17 17:05:46 +0200206 if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100207 {
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000208 vlib_log_err (apm->log_class,
209 "Failed to create AF_PACKET socket: %s (errno %d)",
210 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100211 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
212 goto error;
213 }
Damjan Marionc5170202017-10-11 14:15:47 +0200214
Chaoyu Jinafb19302018-03-13 07:37:41 -0700215 /* bind before rx ring is cfged so we don't receive packets from other interfaces */
Dave Barachb7b92992018-10-17 10:38:51 -0400216 clib_memset (&sll, 0, sizeof (sll));
Chaoyu Jinafb19302018-03-13 07:37:41 -0700217 sll.sll_family = PF_PACKET;
218 sll.sll_protocol = htons (ETH_P_ALL);
219 sll.sll_ifindex = host_if_index;
jackiechen19857fa41602019-05-07 18:59:13 +0800220 if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
Chaoyu Jinafb19302018-03-13 07:37:41 -0700221 {
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000222 vlib_log_err (apm->log_class,
223 "Failed to bind rx packet socket: %s (errno %d)",
224 strerror (errno), errno);
Chaoyu Jinafb19302018-03-13 07:37:41 -0700225 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
226 goto error;
227 }
228
jackiechen19857fa41602019-05-07 18:59:13 +0800229 if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100230 {
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000231 vlib_log_err (apm->log_class,
232 "Failed to set rx packet interface version: %s (errno %d)",
233 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100234 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
235 goto error;
236 }
237
Damjan Marionc5170202017-10-11 14:15:47 +0200238 int opt = 1;
jackiechen19857fa41602019-05-07 18:59:13 +0800239 if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100240 {
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000241 vlib_log_err (
Mohsin Kazmi219cbcb2022-03-18 16:58:31 +0000242 apm->log_class,
243 "Failed to set packet tx ring error handling option: %s (errno %d)",
244 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100245 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
246 goto error;
247 }
248
Mohsin Kazmi788676b2022-04-05 12:43:13 +0000249 if (*flags & AF_PACKET_IF_FLAGS_CKSUM_GSO)
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +0000250 {
Mohsin Kazmi788676b2022-04-05 12:43:13 +0000251
252 int opt2 = 1;
253 if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) <
254 0)
255 {
256 // remove the flag
257 *flags &= ~AF_PACKET_IF_FLAGS_CKSUM_GSO;
258 vlib_log_debug (apm->log_class,
259 "Failed to set packet vnet hdr error handling "
260 "option: %s (errno %d)",
261 strerror (errno), errno);
262 }
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +0000263 }
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +0000264
Florin Coras2dc942e2021-11-22 21:34:56 -0800265#if defined(PACKET_QDISC_BYPASS)
Mohsin Kazmi2b6479c2022-04-05 12:03:47 +0000266 if (*flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS)
267 /* Introduced with Linux 3.14 so the ifdef should eventually be removed */
268 if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) <
269 0)
270 {
271 // remove the flag
272 *flags &= ~AF_PACKET_IF_FLAGS_QDISC_BYPASS;
273 vlib_log_debug (apm->log_class,
274 "Failed to set qdisc bypass error "
275 "handling option: %s (errno %d)",
276 strerror (errno), errno);
277 }
Florin Coras2dc942e2021-11-22 21:34:56 -0800278#endif
Mohammed Hawarieed6fc92021-09-06 11:48:17 +0200279
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000280 if (rx_req)
Damjan Marion83243a02016-02-29 13:09:30 +0100281 {
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000282 if (*flags & AF_PACKET_IF_FLAGS_FANOUT)
283 {
284 int fanout = ((fanout_id & 0xffff) | ((PACKET_FANOUT_HASH) << 16));
285 if (setsockopt (*fd, SOL_PACKET, PACKET_FANOUT, &fanout,
286 sizeof (fanout)) < 0)
287 {
288 // remove the flag
289 *flags &= ~AF_PACKET_IF_FLAGS_FANOUT;
290 vlib_log_err (apm->log_class,
291 "Failed to set fanout options: %s (errno %d)",
292 strerror (errno), errno);
293 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
294 goto error;
295 }
296 }
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000297 if (ver == TPACKET_V2)
298 {
299 req_sz = sizeof (tpacket_req_t);
300 ring_sz += rx_req->req.tp_block_size * rx_req->req.tp_block_nr;
301 }
302 else
303 ring_sz += rx_req->req3.tp_block_size * rx_req->req3.tp_block_nr;
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000304 if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000305 {
306 vlib_log_err (apm->log_class,
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000307 "Failed to set packet rx ring options: %s (errno %d)",
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000308 strerror (errno), errno);
309 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
310 goto error;
311 }
312 }
313
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000314 if (tx_req)
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000315 {
316 if (ver == TPACKET_V2)
317 {
318 req_sz = sizeof (tpacket_req_t);
319 ring_sz += tx_req->req.tp_block_size * tx_req->req.tp_block_nr;
320 }
321 else
322 ring_sz += tx_req->req3.tp_block_size * tx_req->req3.tp_block_nr;
323 if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
324 {
325 vlib_log_err (apm->log_class,
326 "Failed to set packet tx ring options: %s (errno %d)",
327 strerror (errno), errno);
328 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
329 goto error;
330 }
331 }
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000332 ring->ring_start_addr = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE,
333 MAP_SHARED | MAP_LOCKED, *fd, 0);
334 if (ring->ring_start_addr == MAP_FAILED)
335 {
336 vlib_log_err (apm->log_class, "mmap failure: %s (errno %d)",
337 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100338 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
339 goto error;
340 }
341
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000342 ring->ring_size = ring_sz;
Damjan Marion83243a02016-02-29 13:09:30 +0100343
Damjan Marion83243a02016-02-29 13:09:30 +0100344 return 0;
345error:
Dave Barach16ad6ae2016-07-28 17:55:30 -0400346 if (*fd >= 0)
jackiechen19857fa41602019-05-07 18:59:13 +0800347 {
348 close (*fd);
349 *fd = -1;
350 }
Damjan Marion83243a02016-02-29 13:09:30 +0100351 return ret;
352}
353
354int
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000355af_packet_queue_init (vlib_main_t *vm, af_packet_if_t *apif,
356 af_packet_create_if_arg_t *arg,
357 af_packet_queue_t *rx_queue, af_packet_queue_t *tx_queue,
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000358 u8 queue_id)
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000359{
360 af_packet_main_t *apm = &af_packet_main;
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000361 tpacket_req_u_t *rx_req = 0;
362 tpacket_req_u_t *tx_req = 0;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000363 int ret, fd = -1;
364 af_packet_ring_t ring = { 0 };
365 u8 *ring_addr = 0;
366 u32 rx_frames_per_block, tx_frames_per_block;
367 u32 rx_frame_size, tx_frame_size;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000368 u32 i = 0;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000369
370 if (rx_queue)
371 {
372 rx_frames_per_block = arg->rx_frames_per_block ?
373 arg->rx_frames_per_block :
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000374 ((apif->version == TPACKET_V3) ?
375 AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK :
376 AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK_V2);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000377
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000378 rx_frame_size =
379 arg->rx_frame_size ?
380 arg->rx_frame_size :
381 ((apif->version == TPACKET_V3) ? AF_PACKET_DEFAULT_RX_FRAME_SIZE :
382 AF_PACKET_DEFAULT_RX_FRAME_SIZE_V2);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000383 vec_validate (rx_queue->rx_req, 0);
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000384 rx_queue->rx_req->req.tp_block_size =
385 rx_frame_size * rx_frames_per_block;
386 rx_queue->rx_req->req.tp_frame_size = rx_frame_size;
387 rx_queue->rx_req->req.tp_block_nr = (apif->version == TPACKET_V3) ?
388 AF_PACKET_RX_BLOCK_NR :
389 AF_PACKET_RX_BLOCK_NR_V2;
390 rx_queue->rx_req->req.tp_frame_nr =
391 rx_queue->rx_req->req.tp_block_nr * rx_frames_per_block;
392 if (apif->version == TPACKET_V3)
393 {
394 rx_queue->rx_req->req3.tp_retire_blk_tov = 1; // 1 ms block timout
395 rx_queue->rx_req->req3.tp_feature_req_word = 0;
396 rx_queue->rx_req->req3.tp_sizeof_priv = 0;
397 }
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000398 rx_req = rx_queue->rx_req;
399 }
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000400 if (tx_queue)
401 {
402 tx_frames_per_block = arg->tx_frames_per_block ?
403 arg->tx_frames_per_block :
404 AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK;
405 tx_frame_size = arg->tx_frame_size ? arg->tx_frame_size :
406 AF_PACKET_DEFAULT_TX_FRAME_SIZE;
407
408 vec_validate (tx_queue->tx_req, 0);
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000409 tx_queue->tx_req->req.tp_block_size =
410 tx_frame_size * tx_frames_per_block;
411 tx_queue->tx_req->req.tp_frame_size = tx_frame_size;
412 tx_queue->tx_req->req.tp_block_nr = AF_PACKET_TX_BLOCK_NR;
413 tx_queue->tx_req->req.tp_frame_nr =
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000414 AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000415 if (apif->version == TPACKET_V3)
416 {
417 tx_queue->tx_req->req3.tp_retire_blk_tov = 0;
418 tx_queue->tx_req->req3.tp_sizeof_priv = 0;
419 tx_queue->tx_req->req3.tp_feature_req_word = 0;
420 }
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000421 tx_req = tx_queue->tx_req;
422 }
423
Mohsin Kazmi3ce1d142022-04-05 10:46:39 +0000424 if (rx_queue || tx_queue)
425 {
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000426 ret =
427 create_packet_sock (apif->host_if_index, rx_req, tx_req, &fd, &ring,
428 apif->dev_instance, &arg->flags, apif->version);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000429
Mohsin Kazmi3ce1d142022-04-05 10:46:39 +0000430 if (ret != 0)
431 goto error;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000432
Mohsin Kazmi3ce1d142022-04-05 10:46:39 +0000433 vec_add1 (apif->rings, ring);
434 ring_addr = ring.ring_start_addr;
435 }
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000436
437 if (rx_queue)
438 {
439 rx_queue->fd = fd;
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000440 vec_validate (rx_queue->rx_ring, rx_queue->rx_req->req.tp_block_nr - 1);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000441 vec_foreach_index (i, rx_queue->rx_ring)
442 {
443 rx_queue->rx_ring[i] =
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000444 ring_addr + i * rx_queue->rx_req->req.tp_block_size;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000445 }
446
447 rx_queue->next_rx_block = 0;
448 rx_queue->queue_id = queue_id;
449 rx_queue->is_rx_pending = 0;
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000450 ring_addr = ring_addr + rx_queue->rx_req->req.tp_block_size *
451 rx_queue->rx_req->req.tp_block_nr;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000452 }
453
454 if (tx_queue)
455 {
456 tx_queue->fd = fd;
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000457 vec_validate (tx_queue->tx_ring, tx_queue->tx_req->req.tp_block_nr - 1);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000458 vec_foreach_index (i, tx_queue->tx_ring)
459 {
460 tx_queue->tx_ring[i] =
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000461 ring_addr + i * tx_queue->tx_req->req.tp_block_size;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000462 }
463
464 tx_queue->next_tx_frame = 0;
465 tx_queue->queue_id = queue_id;
Mohammed Hawari8a419d52022-02-24 15:19:01 +0100466 tx_queue->is_tx_pending = 0;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000467 clib_spinlock_init (&tx_queue->lockp);
468 }
469
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000470 return 0;
471error:
472 vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id);
Mohsin Kazmi3ce1d142022-04-05 10:46:39 +0000473 if (rx_queue)
474 vec_free (rx_queue->rx_req);
475 if (tx_queue)
476 vec_free (tx_queue->tx_req);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000477 return ret;
478}
479
480int
481af_packet_device_init (vlib_main_t *vm, af_packet_if_t *apif,
482 af_packet_create_if_arg_t *args)
483{
484 af_packet_main_t *apm = &af_packet_main;
485 af_packet_queue_t *rx_queue = 0;
486 af_packet_queue_t *tx_queue = 0;
487 u16 nq = clib_min (args->num_rxqs, args->num_txqs);
488 u16 i = 0;
489 int ret = 0;
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000490
491 // enable fanout feature for multi-rxqs
492 if (args->num_rxqs > 1)
493 args->flags |= AF_PACKET_IF_FLAGS_FANOUT;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000494
495 vec_validate (apif->rx_queues, args->num_rxqs - 1);
496 vec_validate (apif->tx_queues, args->num_txqs - 1);
497
498 for (; i < nq; i++)
499 {
500 rx_queue = vec_elt_at_index (apif->rx_queues, i);
501 tx_queue = vec_elt_at_index (apif->tx_queues, i);
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000502 ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000503 if (ret != 0)
504 goto error;
505 }
506
507 if (args->num_rxqs > args->num_txqs)
508 {
509 for (; i < args->num_rxqs; i++)
510 {
511 rx_queue = vec_elt_at_index (apif->rx_queues, i);
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000512 ret = af_packet_queue_init (vm, apif, args, rx_queue, 0, i);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000513 if (ret != 0)
514 goto error;
515 }
516 }
517 else if (args->num_txqs > args->num_rxqs)
518 {
519 for (; i < args->num_txqs; i++)
520 {
521 tx_queue = vec_elt_at_index (apif->tx_queues, i);
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000522 ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i);
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000523 if (ret != 0)
524 goto error;
525 }
526 }
527
528 apif->num_rxqs = args->num_rxqs;
529 apif->num_txqs = args->num_txqs;
530
531 return 0;
532error:
533 vlib_log_err (apm->log_class, "Failed to init device error");
534 return ret;
535}
536
537int
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200538af_packet_create_if (af_packet_create_if_arg_t *arg)
Damjan Marion83243a02016-02-29 13:09:30 +0100539{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200540 af_packet_main_t *apm = &af_packet_main;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200541 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000542 int fd2 = -1;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200543 struct ifreq ifr;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200544 af_packet_if_t *apif = 0;
Damjan Marion83243a02016-02-29 13:09:30 +0100545 u8 hw_addr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200546 vnet_sw_interface_t *sw;
547 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +0000548 vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200549 uword *p;
Damjan Marion83243a02016-02-29 13:09:30 +0100550 uword if_index;
jackiechen19857fa41602019-05-07 18:59:13 +0800551 u8 *host_if_name_dup = 0;
Ray Kinsellac855b732017-04-21 12:24:43 +0100552 int host_if_index = -1;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000553 int ret = 0;
Damjan Marion83243a02016-02-29 13:09:30 +0100554
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200555 p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
Damjan Marion83243a02016-02-29 13:09:30 +0100556 if (p)
557 {
Mohsin Kazmi43fc6882018-03-22 23:45:23 +0100558 apif = vec_elt_at_index (apm->interfaces, p[0]);
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200559 arg->sw_if_index = apif->sw_if_index;
Mohsin Kazmi43fc6882018-03-22 23:45:23 +0100560 return VNET_API_ERROR_IF_ALREADY_EXISTS;
Damjan Marion83243a02016-02-29 13:09:30 +0100561 }
562
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200563 host_if_name_dup = vec_dup (arg->host_if_name);
564
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200565 /*
566 * make sure host side of interface is 'UP' before binding AF_PACKET
567 * socket on it.
568 */
569 if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
Ray Kinsellac855b732017-04-21 12:24:43 +0100570 {
jackiechen19857fa41602019-05-07 18:59:13 +0800571 vlib_log_debug (apm->log_class,
572 "Failed to create AF_UNIX socket: %s (errno %d)",
573 strerror (errno), errno);
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200574 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
575 goto error;
576 }
577
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200578 clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
579 vec_len (arg->host_if_name));
jackiechen19857fa41602019-05-07 18:59:13 +0800580 if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200581 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200582 vlib_log_debug (
583 apm->log_class,
584 "Failed to retrieve the interface (%s) index: %s (errno %d)",
585 arg->host_if_name, strerror (errno), errno);
jackiechen19857fa41602019-05-07 18:59:13 +0800586 ret = VNET_API_ERROR_INVALID_INTERFACE;
587 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100588 }
589
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200590 host_if_index = ifr.ifr_ifindex;
jackiechen19857fa41602019-05-07 18:59:13 +0800591 if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200592 {
jackiechen19857fa41602019-05-07 18:59:13 +0800593 vlib_log_debug (apm->log_class,
594 "Failed to get the active flag: %s (errno %d)",
595 strerror (errno), errno);
596 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200597 goto error;
598 }
599
600 if (!(ifr.ifr_flags & IFF_UP))
601 {
602 ifr.ifr_flags |= IFF_UP;
jackiechen19857fa41602019-05-07 18:59:13 +0800603 if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200604 {
jackiechen19857fa41602019-05-07 18:59:13 +0800605 vlib_log_debug (apm->log_class,
606 "Failed to set the active flag: %s (errno %d)",
607 strerror (errno), errno);
608 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200609 goto error;
610 }
611 }
612
613 if (fd2 > -1)
jackiechen19857fa41602019-05-07 18:59:13 +0800614 {
615 close (fd2);
616 fd2 = -1;
617 }
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200618
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200619 ret = is_bridge (arg->host_if_name);
Ray Kinsellac855b732017-04-21 12:24:43 +0100620 if (ret == 0) /* is a bridge, ignore state */
621 host_if_index = -1;
622
Damjan Marion83243a02016-02-29 13:09:30 +0100623 /* So far everything looks good, let's create interface */
Damjan Marion048ee2e2016-03-16 22:59:21 +0100624 pool_get (apm->interfaces, apif);
Damjan Marion83243a02016-02-29 13:09:30 +0100625 if_index = apif - apm->interfaces;
626
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000627 apif->dev_instance = if_index;
Ray Kinsellac855b732017-04-21 12:24:43 +0100628 apif->host_if_index = host_if_index;
Ivan Kellybfe737a2016-10-07 18:02:43 +0200629 apif->host_if_name = host_if_name_dup;
Dave Barach13f3c452016-03-29 11:56:41 -0400630 apif->per_interface_next_index = ~0;
Mohsin Kazmicae84fa2021-10-08 15:10:49 +0000631 apif->mode = arg->mode;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000632
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000633 if (arg->is_v2)
634 apif->version = TPACKET_V2;
635 else
636 apif->version = TPACKET_V3;
637
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000638 ret = af_packet_device_init (vm, apif, arg);
639 if (ret != 0)
640 goto error;
Damjan Marion83243a02016-02-29 13:09:30 +0100641
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100642 ret = af_packet_read_mtu (apif);
643 if (ret != 0)
644 goto error;
645
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100646
Nathan Skrzypczakb225b0a2021-10-26 16:11:38 +0200647 if (apif->mode != AF_PACKET_IF_MODE_IP)
Mohsin Kazmicae84fa2021-10-08 15:10:49 +0000648 {
Damjan Marion5c954c42022-01-06 20:36:14 +0100649 vnet_eth_interface_registration_t eir = {};
Mohsin Kazmicae84fa2021-10-08 15:10:49 +0000650 /*use configured or generate random MAC address */
651 if (arg->hw_addr)
652 clib_memcpy (hw_addr, arg->hw_addr, 6);
653 else
654 {
655 f64 now = vlib_time_now (vm);
656 u32 rnd;
657 rnd = (u32) (now * 1e6);
658 rnd = random_u32 (&rnd);
659
660 clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
661 hw_addr[0] = 2;
662 hw_addr[1] = 0xfe;
663 }
664
Damjan Marion5c954c42022-01-06 20:36:14 +0100665 eir.dev_class_index = af_packet_device_class.index;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000666 eir.dev_instance = apif->dev_instance;
Damjan Marion5c954c42022-01-06 20:36:14 +0100667 eir.address = hw_addr;
Damjan Marion1cd0e5d2022-01-17 14:49:17 +0100668 eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
Damjan Marion5c954c42022-01-06 20:36:14 +0100669 apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
Mohsin Kazmicae84fa2021-10-08 15:10:49 +0000670 }
Damjan Marion83243a02016-02-29 13:09:30 +0100671 else
672 {
Mohsin Kazmicae84fa2021-10-08 15:10:49 +0000673 apif->hw_if_index = vnet_register_interface (
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000674 vnm, af_packet_device_class.index, apif->dev_instance,
675 af_packet_ip_device_hw_interface_class.index, apif->dev_instance);
Damjan Marion83243a02016-02-29 13:09:30 +0100676 }
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000677
Damjan Marion83243a02016-02-29 13:09:30 +0100678 sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
679 apif->sw_if_index = sw->sw_if_index;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000680
681 af_packet_set_rx_queues (vm, apif);
682 af_packet_set_tx_queues (vm, apif);
Damjan Marion44036902017-04-28 12:29:15 +0200683
Mohsin Kazmic73984a2022-04-05 13:08:53 +0000684 if (arg->flags & AF_PACKET_IF_FLAGS_FANOUT)
685 apif->is_fanout_enabled = 1;
686
Mohsin Kazmi2b6479c2022-04-05 12:03:47 +0000687 apif->is_qdisc_bypass_enabled =
688 (arg->flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS);
689
Mohsin Kazmi788676b2022-04-05 12:43:13 +0000690 if (arg->flags & AF_PACKET_IF_FLAGS_CKSUM_GSO)
691 apif->is_cksum_gso_enabled = 1;
692
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +0000693 if (apif->is_cksum_gso_enabled)
694 caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM |
695 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
696
697 vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps);
Damjan Marion83243a02016-02-29 13:09:30 +0100698 vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
699 VNET_HW_INTERFACE_FLAG_LINK_UP);
700
Ivan Kellybfe737a2016-10-07 18:02:43 +0200701 mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
702 0);
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200703 arg->sw_if_index = apif->sw_if_index;
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100704
Damjan Marion83243a02016-02-29 13:09:30 +0100705 return 0;
706
707error:
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200708 if (fd2 > -1)
jackiechen19857fa41602019-05-07 18:59:13 +0800709 {
710 close (fd2);
711 fd2 = -1;
712 }
Ivan Kellybfe737a2016-10-07 18:02:43 +0200713 vec_free (host_if_name_dup);
Gabriel Ganne01f6c732022-05-05 10:16:56 +0200714 if (apif)
715 {
716 memset (apif, 0, sizeof (*apif));
717 pool_put (apm->interfaces, apif);
718 }
Damjan Marion83243a02016-02-29 13:09:30 +0100719 return ret;
720}
721
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000722static int
723af_packet_rx_queue_free (af_packet_if_t *apif, af_packet_queue_t *rx_queue)
724{
725 clib_file_del_by_index (&file_main, rx_queue->clib_file_index);
726 close (rx_queue->fd);
727 rx_queue->fd = -1;
728 rx_queue->rx_ring = NULL;
729 vec_free (rx_queue->rx_req);
730 rx_queue->rx_req = NULL;
731 return 0;
732}
733
734static int
735af_packet_tx_queue_free (af_packet_if_t *apif, af_packet_queue_t *tx_queue)
736{
737 close (tx_queue->fd);
738 tx_queue->fd = -1;
739 clib_spinlock_free (&tx_queue->lockp);
740 tx_queue->tx_ring = NULL;
741 vec_free (tx_queue->tx_req);
742 tx_queue->tx_req = NULL;
743 return 0;
744}
745
746static int
747af_packet_ring_free (af_packet_if_t *apif, af_packet_ring_t *ring)
748{
749 af_packet_main_t *apm = &af_packet_main;
750
751 if (ring)
752 {
753 // FIXME: unmap the memory
754 if (munmap (ring->ring_start_addr, ring->ring_size))
755 vlib_log_warn (apm->log_class,
756 "Host interface %s could not free ring %p of size %u",
757 apif->host_if_name, ring->ring_start_addr,
758 ring->ring_size);
759 else
760 ring->ring_start_addr = 0;
761 }
762
763 return 0;
764}
765
Peter Leidba76f22016-04-08 08:16:31 -0700766int
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200767af_packet_delete_if (u8 *host_if_name)
Peter Leidba76f22016-04-08 08:16:31 -0700768{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200769 vnet_main_t *vnm = vnet_get_main ();
Peter Leidba76f22016-04-08 08:16:31 -0700770 af_packet_main_t *apm = &af_packet_main;
771 af_packet_if_t *apif;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000772 af_packet_queue_t *rx_queue;
773 af_packet_queue_t *tx_queue;
774 af_packet_ring_t *ring;
Peter Leidba76f22016-04-08 08:16:31 -0700775 uword *p;
Peter Leidba76f22016-04-08 08:16:31 -0700776
Damjan Marion00a9dca2016-08-17 17:05:46 +0200777 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
778 if (p == NULL)
779 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200780 vlib_log_warn (apm->log_class, "Host interface %s does not exist",
781 host_if_name);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200782 return VNET_API_ERROR_SYSCALL_ERROR_1;
783 }
784 apif = pool_elt_at_index (apm->interfaces, p[0]);
Peter Leidba76f22016-04-08 08:16:31 -0700785
786 /* bring down the interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200787 vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
Mohsin Kazmi530c5ee2022-11-17 14:04:49 +0000788 if (apif->mode != AF_PACKET_IF_MODE_IP)
789 ethernet_delete_interface (vnm, apif->hw_if_index);
790 else
791 vnet_delete_hw_interface (vnm, apif->hw_if_index);
Peter Leidba76f22016-04-08 08:16:31 -0700792
793 /* clean up */
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000794 vec_foreach (rx_queue, apif->rx_queues)
795 af_packet_rx_queue_free (apif, rx_queue);
796 vec_foreach (tx_queue, apif->tx_queues)
797 af_packet_tx_queue_free (apif, tx_queue);
798 vec_foreach (ring, apif->rings)
799 af_packet_ring_free (apif, ring);
Eyal Barif298ecf2016-09-19 18:47:39 +0300800
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000801 vec_free (apif->rx_queues);
802 apif->rx_queues = NULL;
803 vec_free (apif->tx_queues);
804 apif->tx_queues = NULL;
805 vec_free (apif->rings);
806 apif->rings = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700807
Damjan Marion00a9dca2016-08-17 17:05:46 +0200808 vec_free (apif->host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700809 apif->host_if_name = NULL;
Ray Kinsellac855b732017-04-21 12:24:43 +0100810 apif->host_if_index = -1;
Peter Leidba76f22016-04-08 08:16:31 -0700811
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000812 mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p);
Peter Leidba76f22016-04-08 08:16:31 -0700813
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000814 memset (apif, 0, sizeof (*apif));
Damjan Marion00a9dca2016-08-17 17:05:46 +0200815 pool_put (apm->interfaces, apif);
Peter Leidba76f22016-04-08 08:16:31 -0700816
817 return 0;
818}
819
Jakub Grajciar92b02752017-10-20 13:37:28 +0200820int
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200821af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
Jakub Grajciar92b02752017-10-20 13:37:28 +0200822{
Mohsin Kazmic1fd17b2022-03-22 21:40:04 +0000823 // deprecated ...
Jakub Grajciar92b02752017-10-20 13:37:28 +0200824 return 0;
825}
826
Mohsin Kazmi04e0bb22018-05-28 18:55:37 +0200827int
828af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
829{
830 af_packet_main_t *apm = &af_packet_main;
831 af_packet_if_t *apif;
832 af_packet_if_detail_t *r_af_packet_ifs = NULL;
833 af_packet_if_detail_t *af_packet_if = NULL;
834
Damjan Marionb2c31b62020-12-13 21:47:40 +0100835 pool_foreach (apif, apm->interfaces)
836 {
Jakub Grajciar3b2db902019-08-26 11:25:52 +0200837 vec_add2 (r_af_packet_ifs, af_packet_if, 1);
838 af_packet_if->sw_if_index = apif->sw_if_index;
839 if (apif->host_if_name)
840 {
841 clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
842 MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
843 strlen ((const char *) apif->host_if_name)));
844 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100845 }
Mohsin Kazmi04e0bb22018-05-28 18:55:37 +0200846
847 *out_af_packet_ifs = r_af_packet_ifs;
848
849 return 0;
850}
851
Damjan Marion83243a02016-02-29 13:09:30 +0100852static clib_error_t *
853af_packet_init (vlib_main_t * vm)
854{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200855 af_packet_main_t *apm = &af_packet_main;
Damjan Marion553f6bd2016-09-07 11:54:22 +0200856 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion83243a02016-02-29 13:09:30 +0100857
Dave Barachb7b92992018-10-17 10:38:51 -0400858 clib_memset (apm, 0, sizeof (af_packet_main_t));
Damjan Marion83243a02016-02-29 13:09:30 +0100859
860 mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
861
Damjan Marion553f6bd2016-09-07 11:54:22 +0200862 vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
863 CLIB_CACHE_LINE_BYTES);
864
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200865 apm->log_class = vlib_log_register_class ("af_packet", 0);
866 vlib_log_debug (apm->log_class, "initialized");
867
Damjan Marion83243a02016-02-29 13:09:30 +0100868 return 0;
869}
870
871VLIB_INIT_FUNCTION (af_packet_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200872
873/*
874 * fd.io coding-style-patch-verification: ON
875 *
876 * Local Variables:
877 * eval: (c-set-style "gnu")
878 * End:
879 */