devices: add cli support to disable qdisc bypass
[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 32
52 #define AF_PACKET_DEFAULT_RX_FRAME_SIZE       2048
53 #define AF_PACKET_RX_BLOCK_NR                 160
54
55 /*defined in net/if.h but clashes with dpdk headers */
56 unsigned int if_nametoindex (const char *ifname);
57
58 static clib_error_t *
59 af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
60                                   u32 frame_size)
61 {
62   clib_error_t *error, *rv;
63   af_packet_main_t *apm = &af_packet_main;
64   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);
65
66   error = vnet_netlink_set_link_mtu (apif->host_if_index,
67                                      frame_size + hi->frame_overhead);
68
69   if (error)
70     {
71       vlib_log_err (apm->log_class, "netlink failed to change MTU: %U",
72                     format_clib_error, error);
73       rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U",
74                        format_clib_error, error);
75       clib_error_free (error);
76       return rv;
77     }
78   else
79     apif->host_mtu = frame_size + hi->frame_overhead;
80   return 0;
81 }
82
83 static int
84 af_packet_read_mtu (af_packet_if_t *apif)
85 {
86   af_packet_main_t *apm = &af_packet_main;
87   clib_error_t *error;
88   error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
89   if (error)
90     {
91       vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
92                     format_clib_error, error);
93       clib_error_free (error);
94       return VNET_API_ERROR_SYSCALL_ERROR_1;
95     }
96   return 0;
97 }
98
99 static clib_error_t *
100 af_packet_fd_read_ready (clib_file_t * uf)
101 {
102   vnet_main_t *vnm = vnet_get_main ();
103
104   /* Schedule the rx node */
105   vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
106   return 0;
107 }
108
109 static int
110 is_bridge (const u8 * host_if_name)
111 {
112   u8 *s;
113   DIR *dir = NULL;
114
115   s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
116   dir = opendir ((char *) s);
117   vec_free (s);
118
119   if (dir)
120     {
121       closedir (dir);
122       return 0;
123     }
124
125   return -1;
126 }
127
128 static void
129 af_packet_set_rx_queues (vlib_main_t *vm, af_packet_if_t *apif)
130 {
131   vnet_main_t *vnm = vnet_get_main ();
132   af_packet_queue_t *rx_queue;
133
134   vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
135                              af_packet_input_node.index);
136
137   vec_foreach (rx_queue, apif->rx_queues)
138     {
139       rx_queue->queue_index = vnet_hw_if_register_rx_queue (
140         vnm, apif->hw_if_index, rx_queue->queue_id, VNET_HW_IF_RXQ_THREAD_ANY);
141
142       {
143         clib_file_t template = { 0 };
144         template.read_function = af_packet_fd_read_ready;
145         template.file_descriptor = rx_queue->fd;
146         template.private_data = rx_queue->queue_index;
147         template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
148         template.description =
149           format (0, "%U queue %u", format_af_packet_device_name,
150                   apif->dev_instance, rx_queue->queue_id);
151         rx_queue->clib_file_index = clib_file_add (&file_main, &template);
152       }
153       vnet_hw_if_set_rx_queue_file_index (vnm, rx_queue->queue_index,
154                                           rx_queue->clib_file_index);
155       vnet_hw_if_set_rx_queue_mode (vnm, rx_queue->queue_index,
156                                     VNET_HW_IF_RX_MODE_INTERRUPT);
157       rx_queue->mode = VNET_HW_IF_RX_MODE_INTERRUPT;
158     }
159   vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
160 }
161
162 static void
163 af_packet_set_tx_queues (vlib_main_t *vm, af_packet_if_t *apif)
164 {
165   vnet_main_t *vnm = vnet_get_main ();
166   af_packet_main_t *apm = &af_packet_main;
167   af_packet_queue_t *tx_queue;
168
169   vec_foreach (tx_queue, apif->tx_queues)
170     {
171       tx_queue->queue_index = vnet_hw_if_register_tx_queue (
172         vnm, apif->hw_if_index, tx_queue->queue_id);
173     }
174
175   if (apif->num_txqs == 0)
176     {
177       vlib_log_err (apm->log_class, "Interface %U has 0 txq",
178                     format_vnet_hw_if_index_name, vnm, apif->hw_if_index);
179       return;
180     }
181
182   for (u32 j = 0; j < vlib_get_n_threads (); j++)
183     {
184       u32 qi = apif->tx_queues[j % apif->num_txqs].queue_index;
185       vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
186     }
187
188   vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
189 }
190
191 static int
192 create_packet_v3_sock (int host_if_index, tpacket_req3_t *rx_req,
193                        tpacket_req3_t *tx_req, int *fd, af_packet_ring_t *ring,
194                        u8 *is_cksum_gso_enabled, u32 fanout_id, u8 is_fanout,
195                        af_packet_if_flags_t *flags)
196 {
197   af_packet_main_t *apm = &af_packet_main;
198   struct sockaddr_ll sll;
199   socklen_t req_sz = sizeof (tpacket_req3_t);
200   int ret;
201   int ver = TPACKET_V3;
202   u32 ring_sz = 0;
203
204   if (rx_req)
205     ring_sz += rx_req->tp_block_size * rx_req->tp_block_nr;
206
207   if (tx_req)
208     ring_sz += tx_req->tp_block_size * tx_req->tp_block_nr;
209
210   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
211     {
212       vlib_log_err (apm->log_class,
213                     "Failed to create AF_PACKET socket: %s (errno %d)",
214                     strerror (errno), errno);
215       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
216       goto error;
217     }
218
219   /* bind before rx ring is cfged so we don't receive packets from other interfaces */
220   clib_memset (&sll, 0, sizeof (sll));
221   sll.sll_family = PF_PACKET;
222   sll.sll_protocol = htons (ETH_P_ALL);
223   sll.sll_ifindex = host_if_index;
224   if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
225     {
226       vlib_log_err (apm->log_class,
227                     "Failed to bind rx packet socket: %s (errno %d)",
228                     strerror (errno), errno);
229       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
230       goto error;
231     }
232
233   if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
234     {
235       vlib_log_err (apm->log_class,
236                     "Failed to set rx packet interface version: %s (errno %d)",
237                     strerror (errno), errno);
238       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
239       goto error;
240     }
241
242   int opt = 1;
243   if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
244     {
245       vlib_log_err (
246         apm->log_class,
247         "Failed to set packet tx ring error handling option: %s (errno %d)",
248         strerror (errno), errno);
249       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
250       goto error;
251     }
252
253   int opt2 = 1;
254   if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) < 0)
255     {
256       vlib_log_debug (
257         apm->log_class,
258         "Failed to set packet vnet hdr error handling option: %s (errno %d)",
259         strerror (errno), errno);
260     }
261   else
262     *is_cksum_gso_enabled = 1;
263
264 #if defined(PACKET_QDISC_BYPASS)
265   if (*flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS)
266     /* Introduced with Linux 3.14 so the ifdef should eventually be removed  */
267     if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) <
268         0)
269       {
270         // remove the flag
271         *flags &= ~AF_PACKET_IF_FLAGS_QDISC_BYPASS;
272         vlib_log_debug (apm->log_class,
273                         "Failed to set qdisc bypass error "
274                         "handling option: %s (errno %d)",
275                         strerror (errno), errno);
276       }
277 #endif
278
279   if (is_fanout)
280     {
281       int fanout = ((fanout_id & 0xffff) | ((PACKET_FANOUT_HASH) << 16));
282       if (setsockopt (*fd, SOL_PACKET, PACKET_FANOUT, &fanout,
283                       sizeof (fanout)) < 0)
284         {
285           vlib_log_err (apm->log_class,
286                         "Failed to set fanout options: %s (errno %d)",
287                         strerror (errno), errno);
288           ret = VNET_API_ERROR_SYSCALL_ERROR_1;
289           goto error;
290         }
291     }
292
293   if (rx_req)
294     if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
295       {
296         vlib_log_err (apm->log_class,
297                       "Failed to set packet rx ring options: %s (errno %d)",
298                       strerror (errno), errno);
299         ret = VNET_API_ERROR_SYSCALL_ERROR_1;
300         goto error;
301       }
302
303   if (tx_req)
304     if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
305       {
306         vlib_log_err (apm->log_class,
307                       "Failed to set packet tx ring options: %s (errno %d)",
308                       strerror (errno), errno);
309         ret = VNET_API_ERROR_SYSCALL_ERROR_1;
310         goto error;
311       }
312
313   ring->ring_start_addr = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE,
314                                 MAP_SHARED | MAP_LOCKED, *fd, 0);
315   if (ring->ring_start_addr == MAP_FAILED)
316     {
317       vlib_log_err (apm->log_class, "mmap failure: %s (errno %d)",
318                     strerror (errno), errno);
319       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
320       goto error;
321     }
322
323   ring->ring_size = ring_sz;
324
325   return 0;
326 error:
327   if (*fd >= 0)
328     {
329       close (*fd);
330       *fd = -1;
331     }
332   return ret;
333 }
334
335 int
336 af_packet_queue_init (vlib_main_t *vm, af_packet_if_t *apif,
337                       af_packet_create_if_arg_t *arg,
338                       af_packet_queue_t *rx_queue, af_packet_queue_t *tx_queue,
339                       u8 queue_id, u8 is_fanout)
340 {
341   af_packet_main_t *apm = &af_packet_main;
342   tpacket_req3_t *rx_req = 0;
343   tpacket_req3_t *tx_req = 0;
344   int ret, fd = -1;
345   af_packet_ring_t ring = { 0 };
346   u8 *ring_addr = 0;
347   u32 rx_frames_per_block, tx_frames_per_block;
348   u32 rx_frame_size, tx_frame_size;
349   u32 i = 0;
350   u8 is_cksum_gso_enabled = 0;
351
352   if (rx_queue)
353     {
354       rx_frames_per_block = arg->rx_frames_per_block ?
355                                     arg->rx_frames_per_block :
356                                     AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK;
357
358       rx_frame_size = arg->rx_frame_size ? arg->rx_frame_size :
359                                                  AF_PACKET_DEFAULT_RX_FRAME_SIZE;
360       vec_validate (rx_queue->rx_req, 0);
361       rx_queue->rx_req->tp_block_size = rx_frame_size * rx_frames_per_block;
362       rx_queue->rx_req->tp_frame_size = rx_frame_size;
363       rx_queue->rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
364       rx_queue->rx_req->tp_frame_nr =
365         AF_PACKET_RX_BLOCK_NR * rx_frames_per_block;
366       rx_queue->rx_req->tp_retire_blk_tov = 1; // 1 ms block timout
367       rx_queue->rx_req->tp_feature_req_word = 0;
368       rx_queue->rx_req->tp_sizeof_priv = 0;
369       rx_req = rx_queue->rx_req;
370     }
371
372   if (tx_queue)
373     {
374       tx_frames_per_block = arg->tx_frames_per_block ?
375                                     arg->tx_frames_per_block :
376                                     AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK;
377       tx_frame_size = arg->tx_frame_size ? arg->tx_frame_size :
378                                                  AF_PACKET_DEFAULT_TX_FRAME_SIZE;
379
380       vec_validate (tx_queue->tx_req, 0);
381       tx_queue->tx_req->tp_block_size = tx_frame_size * tx_frames_per_block;
382       tx_queue->tx_req->tp_frame_size = tx_frame_size;
383       tx_queue->tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
384       tx_queue->tx_req->tp_frame_nr =
385         AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
386       tx_queue->tx_req->tp_retire_blk_tov = 0;
387       tx_queue->tx_req->tp_sizeof_priv = 0;
388       tx_queue->tx_req->tp_feature_req_word = 0;
389       tx_req = tx_queue->tx_req;
390     }
391
392   if (rx_queue || tx_queue)
393     {
394       ret = create_packet_v3_sock (apif->host_if_index, rx_req, tx_req, &fd,
395                                    &ring, &is_cksum_gso_enabled,
396                                    apif->dev_instance, is_fanout, &arg->flags);
397
398       if (ret != 0)
399         goto error;
400
401       vec_add1 (apif->rings, ring);
402       ring_addr = ring.ring_start_addr;
403     }
404
405   if (rx_queue)
406     {
407       rx_queue->fd = fd;
408       vec_validate (rx_queue->rx_ring, rx_queue->rx_req->tp_block_nr - 1);
409       vec_foreach_index (i, rx_queue->rx_ring)
410         {
411           rx_queue->rx_ring[i] =
412             ring_addr + i * rx_queue->rx_req->tp_block_size;
413         }
414
415       rx_queue->next_rx_block = 0;
416       rx_queue->queue_id = queue_id;
417       rx_queue->is_rx_pending = 0;
418       ring_addr = ring_addr + rx_queue->rx_req->tp_block_size *
419                                 rx_queue->rx_req->tp_block_nr;
420     }
421
422   if (tx_queue)
423     {
424       tx_queue->fd = fd;
425       vec_validate (tx_queue->tx_ring, tx_queue->tx_req->tp_block_nr - 1);
426       vec_foreach_index (i, tx_queue->tx_ring)
427         {
428           tx_queue->tx_ring[i] =
429             ring_addr + i * tx_queue->tx_req->tp_block_size;
430         }
431
432       tx_queue->next_tx_frame = 0;
433       tx_queue->queue_id = queue_id;
434       clib_spinlock_init (&tx_queue->lockp);
435     }
436
437   if (queue_id == 0)
438     apif->is_cksum_gso_enabled = is_cksum_gso_enabled;
439
440   return 0;
441 error:
442   vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id);
443   if (rx_queue)
444     vec_free (rx_queue->rx_req);
445   if (tx_queue)
446     vec_free (tx_queue->tx_req);
447   return ret;
448 }
449
450 int
451 af_packet_device_init (vlib_main_t *vm, af_packet_if_t *apif,
452                        af_packet_create_if_arg_t *args)
453 {
454   af_packet_main_t *apm = &af_packet_main;
455   af_packet_queue_t *rx_queue = 0;
456   af_packet_queue_t *tx_queue = 0;
457   u16 nq = clib_min (args->num_rxqs, args->num_txqs);
458   u16 i = 0;
459   int ret = 0;
460   u8 is_fanout = (args->num_rxqs > 1) ? 1 : 0;
461
462   vec_validate (apif->rx_queues, args->num_rxqs - 1);
463   vec_validate (apif->tx_queues, args->num_txqs - 1);
464
465   for (; i < nq; i++)
466     {
467       rx_queue = vec_elt_at_index (apif->rx_queues, i);
468       tx_queue = vec_elt_at_index (apif->tx_queues, i);
469       ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i,
470                                   is_fanout);
471       if (ret != 0)
472         goto error;
473     }
474
475   if (args->num_rxqs > args->num_txqs)
476     {
477       for (; i < args->num_rxqs; i++)
478         {
479           rx_queue = vec_elt_at_index (apif->rx_queues, i);
480           ret =
481             af_packet_queue_init (vm, apif, args, rx_queue, 0, i, is_fanout);
482           if (ret != 0)
483             goto error;
484         }
485     }
486   else if (args->num_txqs > args->num_rxqs)
487     {
488       for (; i < args->num_txqs; i++)
489         {
490           tx_queue = vec_elt_at_index (apif->tx_queues, i);
491           ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i, 0);
492           if (ret != 0)
493             goto error;
494         }
495     }
496
497   apif->num_rxqs = args->num_rxqs;
498   apif->num_txqs = args->num_txqs;
499
500   return 0;
501 error:
502   vlib_log_err (apm->log_class, "Failed to init device error");
503   return ret;
504 }
505
506 int
507 af_packet_create_if (af_packet_create_if_arg_t *arg)
508 {
509   af_packet_main_t *apm = &af_packet_main;
510   vlib_main_t *vm = vlib_get_main ();
511   int fd2 = -1;
512   struct ifreq ifr;
513   af_packet_if_t *apif = 0;
514   u8 hw_addr[6];
515   vnet_sw_interface_t *sw;
516   vnet_main_t *vnm = vnet_get_main ();
517   vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
518   uword *p;
519   uword if_index;
520   u8 *host_if_name_dup = 0;
521   int host_if_index = -1;
522   int ret = 0;
523
524   p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
525   if (p)
526     {
527       apif = vec_elt_at_index (apm->interfaces, p[0]);
528       arg->sw_if_index = apif->sw_if_index;
529       return VNET_API_ERROR_IF_ALREADY_EXISTS;
530     }
531
532   host_if_name_dup = vec_dup (arg->host_if_name);
533
534   /*
535    * make sure host side of interface is 'UP' before binding AF_PACKET
536    * socket on it.
537    */
538   if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
539     {
540       vlib_log_debug (apm->log_class,
541                       "Failed to create AF_UNIX socket: %s (errno %d)",
542                       strerror (errno), errno);
543       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
544       goto error;
545     }
546
547   clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
548                vec_len (arg->host_if_name));
549   if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
550     {
551       vlib_log_debug (
552         apm->log_class,
553         "Failed to retrieve the interface (%s) index: %s (errno %d)",
554         arg->host_if_name, strerror (errno), errno);
555       ret = VNET_API_ERROR_INVALID_INTERFACE;
556       goto error;
557     }
558
559   host_if_index = ifr.ifr_ifindex;
560   if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
561     {
562       vlib_log_debug (apm->log_class,
563                       "Failed to get the active flag: %s (errno %d)",
564                       strerror (errno), errno);
565       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
566       goto error;
567     }
568
569   if (!(ifr.ifr_flags & IFF_UP))
570     {
571       ifr.ifr_flags |= IFF_UP;
572       if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
573         {
574           vlib_log_debug (apm->log_class,
575                           "Failed to set the active flag: %s (errno %d)",
576                           strerror (errno), errno);
577           ret = VNET_API_ERROR_SYSCALL_ERROR_1;
578           goto error;
579         }
580     }
581
582   if (fd2 > -1)
583     {
584       close (fd2);
585       fd2 = -1;
586     }
587
588   ret = is_bridge (arg->host_if_name);
589   if (ret == 0)                 /* is a bridge, ignore state */
590     host_if_index = -1;
591
592   /* So far everything looks good, let's create interface */
593   pool_get (apm->interfaces, apif);
594   if_index = apif - apm->interfaces;
595
596   apif->dev_instance = if_index;
597   apif->host_if_index = host_if_index;
598   apif->host_if_name = host_if_name_dup;
599   apif->per_interface_next_index = ~0;
600   apif->mode = arg->mode;
601
602   ret = af_packet_device_init (vm, apif, arg);
603   if (ret != 0)
604     goto error;
605
606   ret = af_packet_read_mtu (apif);
607   if (ret != 0)
608     goto error;
609
610
611   if (apif->mode != AF_PACKET_IF_MODE_IP)
612     {
613       vnet_eth_interface_registration_t eir = {};
614       /*use configured or generate random MAC address */
615       if (arg->hw_addr)
616         clib_memcpy (hw_addr, arg->hw_addr, 6);
617       else
618         {
619           f64 now = vlib_time_now (vm);
620           u32 rnd;
621           rnd = (u32) (now * 1e6);
622           rnd = random_u32 (&rnd);
623
624           clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
625           hw_addr[0] = 2;
626           hw_addr[1] = 0xfe;
627         }
628
629       eir.dev_class_index = af_packet_device_class.index;
630       eir.dev_instance = apif->dev_instance;
631       eir.address = hw_addr;
632       eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
633       apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
634     }
635   else
636     {
637       apif->hw_if_index = vnet_register_interface (
638         vnm, af_packet_device_class.index, apif->dev_instance,
639         af_packet_ip_device_hw_interface_class.index, apif->dev_instance);
640     }
641
642   sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
643   apif->sw_if_index = sw->sw_if_index;
644
645   af_packet_set_rx_queues (vm, apif);
646   af_packet_set_tx_queues (vm, apif);
647
648   apif->is_qdisc_bypass_enabled =
649     (arg->flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS);
650
651   if (apif->is_cksum_gso_enabled)
652     caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM |
653             VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
654
655   vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps);
656   vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
657                                VNET_HW_INTERFACE_FLAG_LINK_UP);
658
659   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
660                  0);
661   arg->sw_if_index = apif->sw_if_index;
662
663   return 0;
664
665 error:
666   if (fd2 > -1)
667     {
668       close (fd2);
669       fd2 = -1;
670     }
671   vec_free (host_if_name_dup);
672   memset (apif, 0, sizeof (*apif));
673   pool_put (apm->interfaces, apif);
674   return ret;
675 }
676
677 static int
678 af_packet_rx_queue_free (af_packet_if_t *apif, af_packet_queue_t *rx_queue)
679 {
680   clib_file_del_by_index (&file_main, rx_queue->clib_file_index);
681   close (rx_queue->fd);
682   rx_queue->fd = -1;
683   rx_queue->rx_ring = NULL;
684   vec_free (rx_queue->rx_req);
685   rx_queue->rx_req = NULL;
686   return 0;
687 }
688
689 static int
690 af_packet_tx_queue_free (af_packet_if_t *apif, af_packet_queue_t *tx_queue)
691 {
692   close (tx_queue->fd);
693   tx_queue->fd = -1;
694   clib_spinlock_free (&tx_queue->lockp);
695   tx_queue->tx_ring = NULL;
696   vec_free (tx_queue->tx_req);
697   tx_queue->tx_req = NULL;
698   return 0;
699 }
700
701 static int
702 af_packet_ring_free (af_packet_if_t *apif, af_packet_ring_t *ring)
703 {
704   af_packet_main_t *apm = &af_packet_main;
705
706   if (ring)
707     {
708       // FIXME: unmap the memory
709       if (munmap (ring->ring_start_addr, ring->ring_size))
710         vlib_log_warn (apm->log_class,
711                        "Host interface %s could not free ring %p of size %u",
712                        apif->host_if_name, ring->ring_start_addr,
713                        ring->ring_size);
714       else
715         ring->ring_start_addr = 0;
716     }
717
718   return 0;
719 }
720
721 int
722 af_packet_delete_if (u8 *host_if_name)
723 {
724   vnet_main_t *vnm = vnet_get_main ();
725   af_packet_main_t *apm = &af_packet_main;
726   af_packet_if_t *apif;
727   af_packet_queue_t *rx_queue;
728   af_packet_queue_t *tx_queue;
729   af_packet_ring_t *ring;
730   uword *p;
731
732   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
733   if (p == NULL)
734     {
735       vlib_log_warn (apm->log_class, "Host interface %s does not exist",
736                      host_if_name);
737       return VNET_API_ERROR_SYSCALL_ERROR_1;
738     }
739   apif = pool_elt_at_index (apm->interfaces, p[0]);
740
741   /* bring down the interface */
742   vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
743
744   /* clean up */
745   vec_foreach (rx_queue, apif->rx_queues)
746     af_packet_rx_queue_free (apif, rx_queue);
747   vec_foreach (tx_queue, apif->tx_queues)
748     af_packet_tx_queue_free (apif, tx_queue);
749   vec_foreach (ring, apif->rings)
750     af_packet_ring_free (apif, ring);
751
752   vec_free (apif->rx_queues);
753   apif->rx_queues = NULL;
754   vec_free (apif->tx_queues);
755   apif->tx_queues = NULL;
756   vec_free (apif->rings);
757   apif->rings = NULL;
758
759   vec_free (apif->host_if_name);
760   apif->host_if_name = NULL;
761   apif->host_if_index = -1;
762
763   mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p);
764
765   if (apif->mode != AF_PACKET_IF_MODE_IP)
766     ethernet_delete_interface (vnm, apif->hw_if_index);
767   else
768     vnet_delete_hw_interface (vnm, apif->hw_if_index);
769
770   memset (apif, 0, sizeof (*apif));
771   pool_put (apm->interfaces, apif);
772
773   return 0;
774 }
775
776 int
777 af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
778 {
779   // deprecated ...
780   return 0;
781 }
782
783 int
784 af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
785 {
786   af_packet_main_t *apm = &af_packet_main;
787   af_packet_if_t *apif;
788   af_packet_if_detail_t *r_af_packet_ifs = NULL;
789   af_packet_if_detail_t *af_packet_if = NULL;
790
791   pool_foreach (apif, apm->interfaces)
792      {
793       vec_add2 (r_af_packet_ifs, af_packet_if, 1);
794       af_packet_if->sw_if_index = apif->sw_if_index;
795       if (apif->host_if_name)
796         {
797           clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
798                        MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
799                        strlen ((const char *) apif->host_if_name)));
800         }
801     }
802
803   *out_af_packet_ifs = r_af_packet_ifs;
804
805   return 0;
806 }
807
808 static clib_error_t *
809 af_packet_init (vlib_main_t * vm)
810 {
811   af_packet_main_t *apm = &af_packet_main;
812   vlib_thread_main_t *tm = vlib_get_thread_main ();
813
814   clib_memset (apm, 0, sizeof (af_packet_main_t));
815
816   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
817
818   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
819                         CLIB_CACHE_LINE_BYTES);
820
821   apm->log_class = vlib_log_register_class ("af_packet", 0);
822   vlib_log_debug (apm->log_class, "initialized");
823
824   return 0;
825 }
826
827 VLIB_INIT_FUNCTION (af_packet_init);
828
829 /*
830  * fd.io coding-style-patch-verification: ON
831  *
832  * Local Variables:
833  * eval: (c-set-style "gnu")
834  * End:
835  */