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