blob: ea52878dbcf0abb8ba411efd8714b6deb8eafd13 [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>
Ray Kinsellac855b732017-04-21 12:24:43 +010022#include <dirent.h>
Ray Kinsella7bfa1192017-05-15 11:52:43 +010023#include <sys/stat.h>
24#include <sys/types.h>
25#include <fcntl.h>
Damjan Marion83243a02016-02-29 13:09:30 +010026
27#include <vlib/vlib.h>
28#include <vlib/unix/unix.h>
29#include <vnet/ip/ip.h>
30#include <vnet/ethernet/ethernet.h>
31
32#include <vnet/devices/af_packet/af_packet.h>
33
34#define AF_PACKET_DEBUG_SOCKET 0
35
36#define AF_PACKET_TX_FRAMES_PER_BLOCK 1024
37#define AF_PACKET_TX_FRAME_SIZE (2048 * 5)
38#define AF_PACKET_TX_BLOCK_NR 1
39#define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \
40 AF_PACKET_TX_FRAMES_PER_BLOCK)
41#define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \
42 AF_PACKET_TX_FRAMES_PER_BLOCK)
43
44#define AF_PACKET_RX_FRAMES_PER_BLOCK 1024
45#define AF_PACKET_RX_FRAME_SIZE (2048 * 5)
46#define AF_PACKET_RX_BLOCK_NR 1
47#define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \
48 AF_PACKET_RX_FRAMES_PER_BLOCK)
49#define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \
50 AF_PACKET_RX_FRAMES_PER_BLOCK)
51
52#if AF_PACKET_DEBUG_SOCKET == 1
53#define DBG_SOCK(args...) clib_warning(args);
54#else
55#define DBG_SOCK(args...)
56#endif
57
58/*defined in net/if.h but clashes with dpdk headers */
Damjan Marion00a9dca2016-08-17 17:05:46 +020059unsigned int if_nametoindex (const char *ifname);
Damjan Marion83243a02016-02-29 13:09:30 +010060
61typedef struct tpacket_req tpacket_req_t;
62
63static u32
Damjan Marion00a9dca2016-08-17 17:05:46 +020064af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
65 u32 flags)
Damjan Marion83243a02016-02-29 13:09:30 +010066{
Ray Kinsella7bfa1192017-05-15 11:52:43 +010067 clib_error_t *error;
68 u8 *s;
69 af_packet_main_t *apm = &af_packet_main;
70 af_packet_if_t *apif =
71 pool_elt_at_index (apm->interfaces, hi->dev_instance);
72
73 if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU))
74 {
75 s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
76
77 error = vlib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
78 vec_free (s);
79
80 if (error)
81 {
82 clib_error_report (error);
83 return VNET_API_ERROR_SYSCALL_ERROR_1;
84 }
85 }
86
Damjan Marion83243a02016-02-29 13:09:30 +010087 return 0;
88}
89
Damjan Marion00a9dca2016-08-17 17:05:46 +020090static clib_error_t *
91af_packet_fd_read_ready (unix_file_t * uf)
Damjan Marion83243a02016-02-29 13:09:30 +010092{
Damjan Marion00a9dca2016-08-17 17:05:46 +020093 af_packet_main_t *apm = &af_packet_main;
Damjan Marioneb743fa2017-03-20 16:34:15 +010094 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion83243a02016-02-29 13:09:30 +010095 u32 idx = uf->private_data;
Damjan Marioneb743fa2017-03-20 16:34:15 +010096 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx);
Damjan Marion83243a02016-02-29 13:09:30 +010097
Damjan Marion00a9dca2016-08-17 17:05:46 +020098 apm->pending_input_bitmap =
99 clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
Damjan Marion83243a02016-02-29 13:09:30 +0100100
101 /* Schedule the rx node */
Damjan Marioneb743fa2017-03-20 16:34:15 +0100102 vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100103
104 return 0;
105}
106
107static int
Ray Kinsellac855b732017-04-21 12:24:43 +0100108is_bridge (const u8 * host_if_name)
109{
110 u8 *s;
111 DIR *dir = NULL;
112
113 s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
114 dir = opendir ((char *) s);
115 vec_free (s);
116
117 if (dir)
118 {
119 closedir (dir);
120 return 0;
121 }
122
123 return -1;
124}
125
126static int
127create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200128 tpacket_req_t * tx_req, int *fd, u8 ** ring)
Damjan Marion83243a02016-02-29 13:09:30 +0100129{
130 int ret, err;
131 struct sockaddr_ll sll;
Damjan Marion83243a02016-02-29 13:09:30 +0100132 int ver = TPACKET_V2;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200133 socklen_t req_sz = sizeof (struct tpacket_req);
Damjan Marion83243a02016-02-29 13:09:30 +0100134 u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
Damjan Marion00a9dca2016-08-17 17:05:46 +0200135 tx_req->tp_block_size * tx_req->tp_block_nr;
Damjan Marion83243a02016-02-29 13:09:30 +0100136
Damjan Marion00a9dca2016-08-17 17:05:46 +0200137 if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100138 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200139 DBG_SOCK ("Failed to create socket");
Damjan Marion83243a02016-02-29 13:09:30 +0100140 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
141 goto error;
142 }
143
Damjan Marion00a9dca2016-08-17 17:05:46 +0200144 if ((err =
145 setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100146 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200147 DBG_SOCK ("Failed to set rx packet interface version");
Damjan Marion83243a02016-02-29 13:09:30 +0100148 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
149 goto error;
150 }
151
152 int opt = 1;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200153 if ((err =
154 setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt))) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100155 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200156 DBG_SOCK ("Failed to set packet tx ring error handling option");
Damjan Marion83243a02016-02-29 13:09:30 +0100157 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
158 goto error;
159 }
160
Damjan Marion00a9dca2016-08-17 17:05:46 +0200161 if ((err =
162 setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100163 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200164 DBG_SOCK ("Failed to set packet rx ring options");
Damjan Marion83243a02016-02-29 13:09:30 +0100165 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
166 goto error;
167 }
168
Damjan Marion00a9dca2016-08-17 17:05:46 +0200169 if ((err =
170 setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100171 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200172 DBG_SOCK ("Failed to set packet rx ring options");
Damjan Marion83243a02016-02-29 13:09:30 +0100173 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
174 goto error;
175 }
176
Damjan Marion00a9dca2016-08-17 17:05:46 +0200177 *ring =
178 mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
179 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100180 if (*ring == MAP_FAILED)
181 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200182 DBG_SOCK ("mmap failure");
Damjan Marion83243a02016-02-29 13:09:30 +0100183 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
184 goto error;
185 }
186
Damjan Marion00a9dca2016-08-17 17:05:46 +0200187 memset (&sll, 0, sizeof (sll));
Damjan Marion83243a02016-02-29 13:09:30 +0100188 sll.sll_family = PF_PACKET;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200189 sll.sll_protocol = htons (ETH_P_ALL);
Damjan Marion83243a02016-02-29 13:09:30 +0100190 sll.sll_ifindex = host_if_index;
191
Damjan Marion00a9dca2016-08-17 17:05:46 +0200192 if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100193 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200194 DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
Damjan Marion83243a02016-02-29 13:09:30 +0100195 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
196 goto error;
197 }
198
199 return 0;
200error:
Dave Barach16ad6ae2016-07-28 17:55:30 -0400201 if (*fd >= 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200202 close (*fd);
Peter Leidba76f22016-04-08 08:16:31 -0700203 *fd = -1;
Damjan Marion83243a02016-02-29 13:09:30 +0100204 return ret;
205}
206
207int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200208af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
209 u32 * sw_if_index)
Damjan Marion83243a02016-02-29 13:09:30 +0100210{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200211 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100212 int ret, fd = -1;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200213 struct tpacket_req *rx_req = 0;
214 struct tpacket_req *tx_req = 0;
215 u8 *ring = 0;
216 af_packet_if_t *apif = 0;
Damjan Marion83243a02016-02-29 13:09:30 +0100217 u8 hw_addr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200218 clib_error_t *error;
219 vnet_sw_interface_t *sw;
Damjan Marion44036902017-04-28 12:29:15 +0200220 vnet_hw_interface_t *hw;
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100221 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion00a9dca2016-08-17 17:05:46 +0200222 vnet_main_t *vnm = vnet_get_main ();
223 uword *p;
Damjan Marion83243a02016-02-29 13:09:30 +0100224 uword if_index;
Ivan Kellybfe737a2016-10-07 18:02:43 +0200225 u8 *host_if_name_dup = vec_dup (host_if_name);
Ray Kinsellac855b732017-04-21 12:24:43 +0100226 int host_if_index = -1;
Damjan Marion83243a02016-02-29 13:09:30 +0100227
228 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
229 if (p)
230 {
231 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
232 }
233
Damjan Marion00a9dca2016-08-17 17:05:46 +0200234 vec_validate (rx_req, 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100235 rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
236 rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
237 rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
238 rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR;
239
Damjan Marion00a9dca2016-08-17 17:05:46 +0200240 vec_validate (tx_req, 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100241 tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE;
242 tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE;
243 tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
244 tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
245
Ray Kinsellac855b732017-04-21 12:24:43 +0100246 host_if_index = if_nametoindex ((const char *) host_if_name);
247
248 if (!host_if_index)
249 {
250 DBG_SOCK ("Wrong host interface name");
251 return VNET_API_ERROR_INVALID_INTERFACE;
252 }
253
254 ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
Damjan Marion83243a02016-02-29 13:09:30 +0100255
256 if (ret != 0)
257 goto error;
258
Ray Kinsellac855b732017-04-21 12:24:43 +0100259 ret = is_bridge (host_if_name);
260
261 if (ret == 0) /* is a bridge, ignore state */
262 host_if_index = -1;
263
Damjan Marion83243a02016-02-29 13:09:30 +0100264 /* So far everything looks good, let's create interface */
Damjan Marion048ee2e2016-03-16 22:59:21 +0100265 pool_get (apm->interfaces, apif);
Damjan Marion83243a02016-02-29 13:09:30 +0100266 if_index = apif - apm->interfaces;
267
Ray Kinsellac855b732017-04-21 12:24:43 +0100268 apif->host_if_index = host_if_index;
Damjan Marion83243a02016-02-29 13:09:30 +0100269 apif->fd = fd;
270 apif->rx_ring = ring;
271 apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
272 apif->rx_req = rx_req;
273 apif->tx_req = tx_req;
Ivan Kellybfe737a2016-10-07 18:02:43 +0200274 apif->host_if_name = host_if_name_dup;
Dave Barach13f3c452016-03-29 11:56:41 -0400275 apif->per_interface_next_index = ~0;
Peter Leidba76f22016-04-08 08:16:31 -0700276 apif->next_tx_frame = 0;
277 apif->next_rx_frame = 0;
Damjan Marion83243a02016-02-29 13:09:30 +0100278
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100279 if (tm->n_vlib_mains > 1)
Damjan Marion1927da22017-03-27 17:08:20 +0200280 clib_spinlock_init (&apif->lockp);
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100281
Damjan Marion83243a02016-02-29 13:09:30 +0100282 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200283 unix_file_t template = { 0 };
Damjan Marion83243a02016-02-29 13:09:30 +0100284 template.read_function = af_packet_fd_read_ready;
285 template.file_descriptor = fd;
286 template.private_data = if_index;
287 template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
288 apif->unix_file_index = unix_file_add (&unix_main, &template);
289 }
290
291 /*use configured or generate random MAC address */
292 if (hw_addr_set)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200293 clib_memcpy (hw_addr, hw_addr_set, 6);
Damjan Marion83243a02016-02-29 13:09:30 +0100294 else
295 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200296 f64 now = vlib_time_now (vm);
Damjan Marion83243a02016-02-29 13:09:30 +0100297 u32 rnd;
298 rnd = (u32) (now * 1e6);
299 rnd = random_u32 (&rnd);
300
Damjan Marion00a9dca2016-08-17 17:05:46 +0200301 clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
Damjan Marion83243a02016-02-29 13:09:30 +0100302 hw_addr[0] = 2;
303 hw_addr[1] = 0xfe;
304 }
305
Damjan Marion00a9dca2016-08-17 17:05:46 +0200306 error = ethernet_register_interface (vnm, af_packet_device_class.index,
307 if_index, hw_addr, &apif->hw_if_index,
308 af_packet_eth_flag_change);
Damjan Marion83243a02016-02-29 13:09:30 +0100309
310 if (error)
311 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200312 memset (apif, 0, sizeof (*apif));
313 pool_put (apm->interfaces, apif);
Damjan Marion83243a02016-02-29 13:09:30 +0100314 clib_error_report (error);
315 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
316 goto error;
317 }
318
319 sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +0200320 hw = vnet_get_hw_interface (vnm, apif->hw_if_index);
Damjan Marion83243a02016-02-29 13:09:30 +0100321 apif->sw_if_index = sw->sw_if_index;
Damjan Marion44036902017-04-28 12:29:15 +0200322 vnet_hw_interface_set_input_node (vnm, apif->hw_if_index,
323 af_packet_input_node.index);
Damjan Marion83243a02016-02-29 13:09:30 +0100324
Damjan Marion44036902017-04-28 12:29:15 +0200325 vnet_hw_interface_assign_rx_thread (vnm, apif->hw_if_index, 0, /* queue */
326 ~0 /* any cpu */ );
327
328 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Damjan Marion83243a02016-02-29 13:09:30 +0100329 vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
330 VNET_HW_INTERFACE_FLAG_LINK_UP);
331
Damjan Marion44036902017-04-28 12:29:15 +0200332 vnet_hw_interface_set_rx_mode (vnm, apif->hw_if_index, 0,
333 VNET_HW_INTERFACE_RX_MODE_INTERRUPT);
334
Ivan Kellybfe737a2016-10-07 18:02:43 +0200335 mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
336 0);
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100337 if (sw_if_index)
338 *sw_if_index = apif->sw_if_index;
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100339
Damjan Marion83243a02016-02-29 13:09:30 +0100340 return 0;
341
342error:
Ivan Kellybfe737a2016-10-07 18:02:43 +0200343 vec_free (host_if_name_dup);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200344 vec_free (rx_req);
345 vec_free (tx_req);
Damjan Marion83243a02016-02-29 13:09:30 +0100346 return ret;
347}
348
Peter Leidba76f22016-04-08 08:16:31 -0700349int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200350af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
Peter Leidba76f22016-04-08 08:16:31 -0700351{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200352 vnet_main_t *vnm = vnet_get_main ();
Peter Leidba76f22016-04-08 08:16:31 -0700353 af_packet_main_t *apm = &af_packet_main;
354 af_packet_if_t *apif;
355 uword *p;
356 uword if_index;
357 u32 ring_sz;
358
Damjan Marion00a9dca2016-08-17 17:05:46 +0200359 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
360 if (p == NULL)
361 {
362 clib_warning ("Host interface %s does not exist", host_if_name);
363 return VNET_API_ERROR_SYSCALL_ERROR_1;
364 }
365 apif = pool_elt_at_index (apm->interfaces, p[0]);
Peter Leidba76f22016-04-08 08:16:31 -0700366 if_index = apif - apm->interfaces;
367
368 /* bring down the interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200369 vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
Damjan Marion44036902017-04-28 12:29:15 +0200370 vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0);
Peter Leidba76f22016-04-08 08:16:31 -0700371
372 /* clean up */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200373 if (apif->unix_file_index != ~0)
374 {
375 unix_file_del (&unix_main, unix_main.file_pool + apif->unix_file_index);
376 apif->unix_file_index = ~0;
377 }
Eyal Barif298ecf2016-09-19 18:47:39 +0300378 else
379 close (apif->fd);
380
Peter Leidba76f22016-04-08 08:16:31 -0700381 ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
Damjan Marion00a9dca2016-08-17 17:05:46 +0200382 apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
383 if (munmap (apif->rx_ring, ring_sz))
384 clib_warning ("Host interface %s could not free rx/tx ring",
385 host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700386 apif->rx_ring = NULL;
387 apif->tx_ring = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700388 apif->fd = -1;
389
Damjan Marion00a9dca2016-08-17 17:05:46 +0200390 vec_free (apif->rx_req);
Peter Leidba76f22016-04-08 08:16:31 -0700391 apif->rx_req = NULL;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200392 vec_free (apif->tx_req);
Peter Leidba76f22016-04-08 08:16:31 -0700393 apif->tx_req = NULL;
394
Damjan Marion00a9dca2016-08-17 17:05:46 +0200395 vec_free (apif->host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700396 apif->host_if_name = NULL;
Ray Kinsellac855b732017-04-21 12:24:43 +0100397 apif->host_if_index = -1;
Peter Leidba76f22016-04-08 08:16:31 -0700398
Damjan Marion00a9dca2016-08-17 17:05:46 +0200399 mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);
Peter Leidba76f22016-04-08 08:16:31 -0700400
Damjan Marion00a9dca2016-08-17 17:05:46 +0200401 ethernet_delete_interface (vnm, apif->hw_if_index);
Peter Leidba76f22016-04-08 08:16:31 -0700402
Damjan Marion00a9dca2016-08-17 17:05:46 +0200403 pool_put (apm->interfaces, apif);
Peter Leidba76f22016-04-08 08:16:31 -0700404
405 return 0;
406}
407
Damjan Marion83243a02016-02-29 13:09:30 +0100408static clib_error_t *
409af_packet_init (vlib_main_t * vm)
410{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200411 af_packet_main_t *apm = &af_packet_main;
Damjan Marion553f6bd2016-09-07 11:54:22 +0200412 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion83243a02016-02-29 13:09:30 +0100413
414 memset (apm, 0, sizeof (af_packet_main_t));
415
416 mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
417
Damjan Marion553f6bd2016-09-07 11:54:22 +0200418 vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
419 CLIB_CACHE_LINE_BYTES);
420
Damjan Marion83243a02016-02-29 13:09:30 +0100421 return 0;
422}
423
424VLIB_INIT_FUNCTION (af_packet_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200425
426/*
427 * fd.io coding-style-patch-verification: ON
428 *
429 * Local Variables:
430 * eval: (c-set-style "gnu")
431 * End:
432 */