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