hs-test: small improvements
[vpp.git] / src / vnet / devices / af_packet / af_packet.c
1 /*
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>
22 #include <sys/ioctl.h>
23 #include <net/if.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28
29 #include <vppinfra/linux/sysfs.h>
30 #include <vlib/vlib.h>
31 #include <vlib/unix/unix.h>
32 #include <vnet/ip/ip.h>
33 #include <vnet/devices/netlink.h>
34 #include <vnet/ethernet/ethernet.h>
35 #include <vnet/interface/rx_queue_funcs.h>
36 #include <vnet/interface/tx_queue_funcs.h>
37
38 #include <vnet/devices/af_packet/af_packet.h>
39
40 af_packet_main_t af_packet_main;
41
42 VNET_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
47 #define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024
48 #define AF_PACKET_DEFAULT_TX_FRAME_SIZE       (2048 * 33) // GSO packet of 64KB
49 #define AF_PACKET_TX_BLOCK_NR           1
50
51 #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
55 #define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 32
56 #define AF_PACKET_DEFAULT_RX_FRAME_SIZE       2048
57 #define AF_PACKET_RX_BLOCK_NR                 160
58
59 /*defined in net/if.h but clashes with dpdk headers */
60 unsigned int if_nametoindex (const char *ifname);
61
62 static clib_error_t *
63 af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
64                                   u32 frame_size)
65 {
66   clib_error_t *error, *rv;
67   af_packet_main_t *apm = &af_packet_main;
68   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);
69
70   error = vnet_netlink_set_link_mtu (apif->host_if_index,
71                                      frame_size + hi->frame_overhead);
72
73   if (error)
74     {
75       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;
81     }
82   else
83     apif->host_mtu = frame_size + hi->frame_overhead;
84   return 0;
85 }
86
87 static int
88 af_packet_read_mtu (af_packet_if_t *apif)
89 {
90   af_packet_main_t *apm = &af_packet_main;
91   clib_error_t *error;
92   error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
93   if (error)
94     {
95       vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
96                     format_clib_error, error);
97       clib_error_free (error);
98       return VNET_API_ERROR_SYSCALL_ERROR_1;
99     }
100   return 0;
101 }
102
103 static clib_error_t *
104 af_packet_fd_read_ready (clib_file_t * uf)
105 {
106   vnet_main_t *vnm = vnet_get_main ();
107
108   /* Schedule the rx node */
109   vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
110   return 0;
111 }
112
113 static int
114 is_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
132 static void
133 af_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
166 static void
167 af_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
195 static int
196 create_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)
199 {
200   af_packet_main_t *apm = &af_packet_main;
201   struct sockaddr_ll sll;
202   socklen_t req_sz = sizeof (tpacket_req3_t);
203   int ret;
204   u32 ring_sz = 0;
205
206   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
207     {
208       vlib_log_err (apm->log_class,
209                     "Failed to create AF_PACKET socket: %s (errno %d)",
210                     strerror (errno), errno);
211       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
212       goto error;
213     }
214
215   /* bind before rx ring is cfged so we don't receive packets from other interfaces */
216   clib_memset (&sll, 0, sizeof (sll));
217   sll.sll_family = PF_PACKET;
218   sll.sll_protocol = htons (ETH_P_ALL);
219   sll.sll_ifindex = host_if_index;
220   if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
221     {
222       vlib_log_err (apm->log_class,
223                     "Failed to bind rx packet socket: %s (errno %d)",
224                     strerror (errno), errno);
225       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
226       goto error;
227     }
228
229   if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
230     {
231       vlib_log_err (apm->log_class,
232                     "Failed to set rx packet interface version: %s (errno %d)",
233                     strerror (errno), errno);
234       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
235       goto error;
236     }
237
238   int opt = 1;
239   if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
240     {
241       vlib_log_err (
242         apm->log_class,
243         "Failed to set packet tx ring error handling option: %s (errno %d)",
244         strerror (errno), errno);
245       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
246       goto error;
247     }
248
249   if (*flags & AF_PACKET_IF_FLAGS_CKSUM_GSO)
250     {
251
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         }
263     }
264
265 #if defined(PACKET_QDISC_BYPASS)
266   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       }
278 #endif
279
280   if (rx_req)
281     {
282       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         }
297       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;
304       if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
305         {
306           vlib_log_err (apm->log_class,
307                         "Failed to set packet rx ring options: %s (errno %d)",
308                         strerror (errno), errno);
309           ret = VNET_API_ERROR_SYSCALL_ERROR_1;
310           goto error;
311         }
312     }
313
314   if (tx_req)
315     {
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     }
332   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);
338       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
339       goto error;
340     }
341
342   ring->ring_size = ring_sz;
343
344   return 0;
345 error:
346   if (*fd >= 0)
347     {
348       close (*fd);
349       *fd = -1;
350     }
351   return ret;
352 }
353
354 int
355 af_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,
358                       u8 queue_id)
359 {
360   af_packet_main_t *apm = &af_packet_main;
361   tpacket_req_u_t *rx_req = 0;
362   tpacket_req_u_t *tx_req = 0;
363   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;
368   u32 i = 0;
369
370   if (rx_queue)
371     {
372       rx_frames_per_block = arg->rx_frames_per_block ?
373                                     arg->rx_frames_per_block :
374                                     ((apif->version == TPACKET_V3) ?
375                                        AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK :
376                                        AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK_V2);
377
378       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);
383       vec_validate (rx_queue->rx_req, 0);
384       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         }
398       rx_req = rx_queue->rx_req;
399     }
400   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);
409       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 =
414         AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
415       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         }
421       tx_req = tx_queue->tx_req;
422     }
423
424   if (rx_queue || tx_queue)
425     {
426       ret =
427         create_packet_sock (apif->host_if_index, rx_req, tx_req, &fd, &ring,
428                             apif->dev_instance, &arg->flags, apif->version);
429
430       if (ret != 0)
431         goto error;
432
433       vec_add1 (apif->rings, ring);
434       ring_addr = ring.ring_start_addr;
435     }
436
437   if (rx_queue)
438     {
439       rx_queue->fd = fd;
440       vec_validate (rx_queue->rx_ring, rx_queue->rx_req->req.tp_block_nr - 1);
441       vec_foreach_index (i, rx_queue->rx_ring)
442         {
443           rx_queue->rx_ring[i] =
444             ring_addr + i * rx_queue->rx_req->req.tp_block_size;
445         }
446
447       rx_queue->next_rx_block = 0;
448       rx_queue->queue_id = queue_id;
449       rx_queue->is_rx_pending = 0;
450       ring_addr = ring_addr + rx_queue->rx_req->req.tp_block_size *
451                                 rx_queue->rx_req->req.tp_block_nr;
452     }
453
454   if (tx_queue)
455     {
456       tx_queue->fd = fd;
457       vec_validate (tx_queue->tx_ring, tx_queue->tx_req->req.tp_block_nr - 1);
458       vec_foreach_index (i, tx_queue->tx_ring)
459         {
460           tx_queue->tx_ring[i] =
461             ring_addr + i * tx_queue->tx_req->req.tp_block_size;
462         }
463
464       tx_queue->next_tx_frame = 0;
465       tx_queue->queue_id = queue_id;
466       tx_queue->is_tx_pending = 0;
467       clib_spinlock_init (&tx_queue->lockp);
468     }
469
470   return 0;
471 error:
472   vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id);
473   if (rx_queue)
474     vec_free (rx_queue->rx_req);
475   if (tx_queue)
476     vec_free (tx_queue->tx_req);
477   return ret;
478 }
479
480 int
481 af_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;
490
491   // enable fanout feature for multi-rxqs
492   if (args->num_rxqs > 1)
493     args->flags |= AF_PACKET_IF_FLAGS_FANOUT;
494
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);
502       ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i);
503       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);
512           ret = af_packet_queue_init (vm, apif, args, rx_queue, 0, i);
513           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);
522           ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i);
523           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;
532 error:
533   vlib_log_err (apm->log_class, "Failed to init device error");
534   return ret;
535 }
536
537 int
538 af_packet_create_if (af_packet_create_if_arg_t *arg)
539 {
540   af_packet_main_t *apm = &af_packet_main;
541   vlib_main_t *vm = vlib_get_main ();
542   int fd2 = -1;
543   struct ifreq ifr;
544   af_packet_if_t *apif = 0;
545   u8 hw_addr[6];
546   vnet_sw_interface_t *sw;
547   vnet_main_t *vnm = vnet_get_main ();
548   vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
549   uword *p;
550   uword if_index;
551   u8 *host_if_name_dup = 0;
552   int host_if_index = -1;
553   int ret = 0;
554
555   p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
556   if (p)
557     {
558       apif = vec_elt_at_index (apm->interfaces, p[0]);
559       arg->sw_if_index = apif->sw_if_index;
560       return VNET_API_ERROR_IF_ALREADY_EXISTS;
561     }
562
563   host_if_name_dup = vec_dup (arg->host_if_name);
564
565   /*
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)
570     {
571       vlib_log_debug (apm->log_class,
572                       "Failed to create AF_UNIX socket: %s (errno %d)",
573                       strerror (errno), errno);
574       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
575       goto error;
576     }
577
578   clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
579                vec_len (arg->host_if_name));
580   if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
581     {
582       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);
586       ret = VNET_API_ERROR_INVALID_INTERFACE;
587       goto error;
588     }
589
590   host_if_index = ifr.ifr_ifindex;
591   if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
592     {
593       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;
597       goto error;
598     }
599
600   if (!(ifr.ifr_flags & IFF_UP))
601     {
602       ifr.ifr_flags |= IFF_UP;
603       if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
604         {
605           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;
609           goto error;
610         }
611     }
612
613   if (fd2 > -1)
614     {
615       close (fd2);
616       fd2 = -1;
617     }
618
619   ret = is_bridge (arg->host_if_name);
620   if (ret == 0)                 /* is a bridge, ignore state */
621     host_if_index = -1;
622
623   /* So far everything looks good, let's create interface */
624   pool_get (apm->interfaces, apif);
625   if_index = apif - apm->interfaces;
626
627   apif->dev_instance = if_index;
628   apif->host_if_index = host_if_index;
629   apif->host_if_name = host_if_name_dup;
630   apif->per_interface_next_index = ~0;
631   apif->mode = arg->mode;
632
633   if (arg->is_v2)
634     apif->version = TPACKET_V2;
635   else
636     apif->version = TPACKET_V3;
637
638   ret = af_packet_device_init (vm, apif, arg);
639   if (ret != 0)
640     goto error;
641
642   ret = af_packet_read_mtu (apif);
643   if (ret != 0)
644     goto error;
645
646
647   if (apif->mode != AF_PACKET_IF_MODE_IP)
648     {
649       vnet_eth_interface_registration_t eir = {};
650       /*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
665       eir.dev_class_index = af_packet_device_class.index;
666       eir.dev_instance = apif->dev_instance;
667       eir.address = hw_addr;
668       eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
669       apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
670     }
671   else
672     {
673       apif->hw_if_index = vnet_register_interface (
674         vnm, af_packet_device_class.index, apif->dev_instance,
675         af_packet_ip_device_hw_interface_class.index, apif->dev_instance);
676     }
677
678   sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
679   apif->sw_if_index = sw->sw_if_index;
680
681   af_packet_set_rx_queues (vm, apif);
682   af_packet_set_tx_queues (vm, apif);
683
684   if (arg->flags & AF_PACKET_IF_FLAGS_FANOUT)
685     apif->is_fanout_enabled = 1;
686
687   apif->is_qdisc_bypass_enabled =
688     (arg->flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS);
689
690   if (arg->flags & AF_PACKET_IF_FLAGS_CKSUM_GSO)
691     apif->is_cksum_gso_enabled = 1;
692
693   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);
698   vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
699                                VNET_HW_INTERFACE_FLAG_LINK_UP);
700
701   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
702                  0);
703   arg->sw_if_index = apif->sw_if_index;
704
705   return 0;
706
707 error:
708   if (fd2 > -1)
709     {
710       close (fd2);
711       fd2 = -1;
712     }
713   vec_free (host_if_name_dup);
714   if (apif)
715     {
716       memset (apif, 0, sizeof (*apif));
717       pool_put (apm->interfaces, apif);
718     }
719   return ret;
720 }
721
722 static int
723 af_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
734 static int
735 af_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
746 static int
747 af_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
766 int
767 af_packet_delete_if (u8 *host_if_name)
768 {
769   vnet_main_t *vnm = vnet_get_main ();
770   af_packet_main_t *apm = &af_packet_main;
771   af_packet_if_t *apif;
772   af_packet_queue_t *rx_queue;
773   af_packet_queue_t *tx_queue;
774   af_packet_ring_t *ring;
775   uword *p;
776
777   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
778   if (p == NULL)
779     {
780       vlib_log_warn (apm->log_class, "Host interface %s does not exist",
781                      host_if_name);
782       return VNET_API_ERROR_SYSCALL_ERROR_1;
783     }
784   apif = pool_elt_at_index (apm->interfaces, p[0]);
785
786   /* bring down the interface */
787   vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
788   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);
792
793   /* clean up */
794   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);
800
801   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;
807
808   vec_free (apif->host_if_name);
809   apif->host_if_name = NULL;
810   apif->host_if_index = -1;
811
812   mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p);
813
814   memset (apif, 0, sizeof (*apif));
815   pool_put (apm->interfaces, apif);
816
817   return 0;
818 }
819
820 int
821 af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
822 {
823   // deprecated ...
824   return 0;
825 }
826
827 int
828 af_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
835   pool_foreach (apif, apm->interfaces)
836      {
837       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         }
845     }
846
847   *out_af_packet_ifs = r_af_packet_ifs;
848
849   return 0;
850 }
851
852 static clib_error_t *
853 af_packet_init (vlib_main_t * vm)
854 {
855   af_packet_main_t *apm = &af_packet_main;
856   vlib_thread_main_t *tm = vlib_get_thread_main ();
857
858   clib_memset (apm, 0, sizeof (af_packet_main_t));
859
860   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
861
862   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
863                         CLIB_CACHE_LINE_BYTES);
864
865   apm->log_class = vlib_log_register_class ("af_packet", 0);
866   vlib_log_debug (apm->log_class, "initialized");
867
868   return 0;
869 }
870
871 VLIB_INIT_FUNCTION (af_packet_init);
872
873 /*
874  * fd.io coding-style-patch-verification: ON
875  *
876  * Local Variables:
877  * eval: (c-set-style "gnu")
878  * End:
879  */