VPP-1001 - update AF Packet Driver to for modern kernels
[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 <dirent.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <linux/virtio_net.h>
27
28 #include <vppinfra/linux/sysfs.h>
29 #include <vlib/vlib.h>
30 #include <vlib/unix/unix.h>
31 #include <vnet/ip/ip.h>
32 #include <vnet/ethernet/ethernet.h>
33
34 #include <vnet/devices/af_packet/af_packet.h>
35
36 #define AF_PACKET_DEBUG_SOCKET          0
37
38 #define AF_PACKET_TX_FRAMES_PER_BLOCK   1024
39 #define AF_PACKET_TX_FRAME_SIZE         (2048 * 5)
40 #define AF_PACKET_TX_BLOCK_NR           1
41 #define AF_PACKET_TX_FRAME_NR           (AF_PACKET_TX_BLOCK_NR * \
42                                          AF_PACKET_TX_FRAMES_PER_BLOCK)
43 #define AF_PACKET_TX_BLOCK_SIZE         (AF_PACKET_TX_FRAME_SIZE * \
44                                          AF_PACKET_TX_FRAMES_PER_BLOCK)
45
46 #define AF_PACKET_RX_FRAMES_PER_BLOCK   1024
47 #define AF_PACKET_RX_FRAME_SIZE         (2048 * 5)
48 #define AF_PACKET_RX_BLOCK_NR           1
49 #define AF_PACKET_RX_FRAME_NR           (AF_PACKET_RX_BLOCK_NR * \
50                                          AF_PACKET_RX_FRAMES_PER_BLOCK)
51 #define AF_PACKET_RX_BLOCK_SIZE         (AF_PACKET_RX_FRAME_SIZE * \
52                                          AF_PACKET_RX_FRAMES_PER_BLOCK)
53
54 #if AF_PACKET_DEBUG_SOCKET == 1
55 #define DBG_SOCK(args...) clib_warning(args);
56 #else
57 #define DBG_SOCK(args...)
58 #endif
59
60 /*defined in net/if.h but clashes with dpdk headers */
61 unsigned int if_nametoindex (const char *ifname);
62
63 typedef struct tpacket_req tpacket_req_t;
64
65 static u32
66 af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
67                            u32 flags)
68 {
69   clib_error_t *error;
70   u8 *s;
71   af_packet_main_t *apm = &af_packet_main;
72   af_packet_if_t *apif =
73     pool_elt_at_index (apm->interfaces, hi->dev_instance);
74
75   if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU))
76     {
77       s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
78
79       error = clib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
80       vec_free (s);
81
82       if (error)
83         {
84           clib_error_report (error);
85           return VNET_API_ERROR_SYSCALL_ERROR_1;
86         }
87     }
88
89   return 0;
90 }
91
92 static clib_error_t *
93 af_packet_fd_read_ready (clib_file_t * uf)
94 {
95   af_packet_main_t *apm = &af_packet_main;
96   vnet_main_t *vnm = vnet_get_main ();
97   u32 idx = uf->private_data;
98   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx);
99
100   apm->pending_input_bitmap =
101     clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
102
103   /* Schedule the rx node */
104   vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0);
105
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 int
129 create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
130                        tpacket_req_t * tx_req, int *fd, u8 ** ring)
131 {
132   af_packet_main_t *apm = &af_packet_main;
133   int ret, err;
134   struct sockaddr_ll sll;
135   int ver = TPACKET_V2;
136   socklen_t req_sz = sizeof (struct tpacket_req);
137   u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
138     tx_req->tp_block_size * tx_req->tp_block_nr;
139
140   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
141     {
142       DBG_SOCK ("Failed to create socket");
143       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
144       goto error;
145     }
146   int opt = 1;
147   if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt, sizeof (opt)) != 0)
148     {
149       DBG_SOCK ("Failed to enable vnet headers on the socket");
150       if ((apm->flags & AF_PACKET_USES_VNET_HEADERS) != 0)
151         {
152           /* Should never happen - vnet was already enabled once,
153            * but we fail to reenable it on a new interface
154            **/
155           ret = VNET_API_ERROR_SYSCALL_ERROR_1;
156           goto error;
157         }
158     }
159   else
160     {
161       apm->flags |= AF_PACKET_USES_VNET_HEADERS;
162     }
163 #ifdef PACKET_QDISC_BYPASS
164   opt = 1;
165   if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) !=
166       0)
167     {
168       DBG_SOCK ("Failed to bypass Linux QDISC");
169     }
170 #endif
171   if ((err =
172        setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
173     {
174       DBG_SOCK ("Failed to set rx packet interface version");
175       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
176       goto error;
177     }
178
179   opt = 1;
180   if ((err =
181        setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt))) < 0)
182     {
183       DBG_SOCK ("Failed to set packet tx ring error handling option");
184       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
185       goto error;
186     }
187
188   if ((err =
189        setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
190     {
191       DBG_SOCK ("Failed to set packet rx ring options");
192       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
193       goto error;
194     }
195
196   if ((err =
197        setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
198     {
199       DBG_SOCK ("Failed to set packet rx ring options");
200       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
201       goto error;
202     }
203
204   *ring =
205     mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
206           0);
207   if (*ring == MAP_FAILED)
208     {
209       DBG_SOCK ("mmap failure");
210       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
211       goto error;
212     }
213
214   memset (&sll, 0, sizeof (sll));
215   sll.sll_family = PF_PACKET;
216   sll.sll_protocol = htons (ETH_P_ALL);
217   sll.sll_ifindex = host_if_index;
218
219   if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
220     {
221       DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
222       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
223       goto error;
224     }
225
226   return 0;
227 error:
228   if (*fd >= 0)
229     close (*fd);
230   *fd = -1;
231   return ret;
232 }
233
234 int
235 af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
236                      u32 * sw_if_index)
237 {
238   af_packet_main_t *apm = &af_packet_main;
239   int ret, fd = -1;
240   struct tpacket_req *rx_req = 0;
241   struct tpacket_req *tx_req = 0;
242   u8 *ring = 0;
243   af_packet_if_t *apif = 0;
244   u8 hw_addr[6];
245   clib_error_t *error;
246   vnet_sw_interface_t *sw;
247   vnet_hw_interface_t *hw;
248   vlib_thread_main_t *tm = vlib_get_thread_main ();
249   vnet_main_t *vnm = vnet_get_main ();
250   uword *p;
251   uword if_index;
252   u8 *host_if_name_dup = vec_dup (host_if_name);
253   int host_if_index = -1;
254
255   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
256   if (p)
257     {
258       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
259     }
260
261   vec_validate (rx_req, 0);
262   rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
263   rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
264   rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
265   rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR;
266
267   vec_validate (tx_req, 0);
268   tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE;
269   tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE;
270   tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
271   tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
272
273   host_if_index = if_nametoindex ((const char *) host_if_name);
274
275   if (!host_if_index)
276     {
277       DBG_SOCK ("Wrong host interface name");
278       return VNET_API_ERROR_INVALID_INTERFACE;
279     }
280
281   ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
282
283   if (ret != 0)
284     goto error;
285
286   ret = is_bridge (host_if_name);
287
288   if (ret == 0)                 /* is a bridge, ignore state */
289     host_if_index = -1;
290
291   /* So far everything looks good, let's create interface */
292   pool_get (apm->interfaces, apif);
293   if_index = apif - apm->interfaces;
294
295   apif->host_if_index = host_if_index;
296   apif->fd = fd;
297   apif->rx_ring = ring;
298   apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
299   apif->rx_req = rx_req;
300   apif->tx_req = tx_req;
301   apif->host_if_name = host_if_name_dup;
302   apif->per_interface_next_index = ~0;
303   apif->next_tx_frame = 0;
304   apif->next_rx_frame = 0;
305
306   if (tm->n_vlib_mains > 1)
307     clib_spinlock_init (&apif->lockp);
308
309   {
310     clib_file_t template = { 0 };
311     template.read_function = af_packet_fd_read_ready;
312     template.file_descriptor = fd;
313     template.private_data = if_index;
314     template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
315     apif->clib_file_index = clib_file_add (&file_main, &template);
316   }
317
318   /*use configured or generate random MAC address */
319   if (hw_addr_set)
320     clib_memcpy (hw_addr, hw_addr_set, 6);
321   else
322     {
323       f64 now = vlib_time_now (vm);
324       u32 rnd;
325       rnd = (u32) (now * 1e6);
326       rnd = random_u32 (&rnd);
327
328       clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
329       hw_addr[0] = 2;
330       hw_addr[1] = 0xfe;
331     }
332
333   error = ethernet_register_interface (vnm, af_packet_device_class.index,
334                                        if_index, hw_addr, &apif->hw_if_index,
335                                        af_packet_eth_flag_change);
336
337   if (error)
338     {
339       memset (apif, 0, sizeof (*apif));
340       pool_put (apm->interfaces, apif);
341       clib_error_report (error);
342       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
343       goto error;
344     }
345
346   sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
347   hw = vnet_get_hw_interface (vnm, apif->hw_if_index);
348   apif->sw_if_index = sw->sw_if_index;
349   vnet_hw_interface_set_input_node (vnm, apif->hw_if_index,
350                                     af_packet_input_node.index);
351
352   vnet_hw_interface_assign_rx_thread (vnm, apif->hw_if_index, 0,        /* queue */
353                                       ~0 /* any cpu */ );
354
355   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
356   vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
357                                VNET_HW_INTERFACE_FLAG_LINK_UP);
358
359   vnet_hw_interface_set_rx_mode (vnm, apif->hw_if_index, 0,
360                                  VNET_HW_INTERFACE_RX_MODE_INTERRUPT);
361
362   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
363                  0);
364   if (sw_if_index)
365     *sw_if_index = apif->sw_if_index;
366
367   return 0;
368
369 error:
370   vec_free (host_if_name_dup);
371   vec_free (rx_req);
372   vec_free (tx_req);
373   return ret;
374 }
375
376 int
377 af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
378 {
379   vnet_main_t *vnm = vnet_get_main ();
380   af_packet_main_t *apm = &af_packet_main;
381   af_packet_if_t *apif;
382   uword *p;
383   uword if_index;
384   u32 ring_sz;
385
386   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
387   if (p == NULL)
388     {
389       clib_warning ("Host interface %s does not exist", host_if_name);
390       return VNET_API_ERROR_SYSCALL_ERROR_1;
391     }
392   apif = pool_elt_at_index (apm->interfaces, p[0]);
393   if_index = apif - apm->interfaces;
394
395   /* bring down the interface */
396   vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
397   vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0);
398
399   /* clean up */
400   if (apif->clib_file_index != ~0)
401     {
402       clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index);
403       apif->clib_file_index = ~0;
404     }
405   else
406     close (apif->fd);
407
408   ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
409     apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
410   if (munmap (apif->rx_ring, ring_sz))
411     clib_warning ("Host interface %s could not free rx/tx ring",
412                   host_if_name);
413   apif->rx_ring = NULL;
414   apif->tx_ring = NULL;
415   apif->fd = -1;
416
417   vec_free (apif->rx_req);
418   apif->rx_req = NULL;
419   vec_free (apif->tx_req);
420   apif->tx_req = NULL;
421
422   vec_free (apif->host_if_name);
423   apif->host_if_name = NULL;
424   apif->host_if_index = -1;
425
426   mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);
427
428   ethernet_delete_interface (vnm, apif->hw_if_index);
429
430   pool_put (apm->interfaces, apif);
431
432   return 0;
433 }
434
435 static clib_error_t *
436 af_packet_init (vlib_main_t * vm)
437 {
438   af_packet_main_t *apm = &af_packet_main;
439   vlib_thread_main_t *tm = vlib_get_thread_main ();
440
441   memset (apm, 0, sizeof (af_packet_main_t));
442
443   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
444
445   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
446                         CLIB_CACHE_LINE_BYTES);
447
448   return 0;
449 }
450
451 VLIB_INIT_FUNCTION (af_packet_init);
452
453 /*
454  * fd.io coding-style-patch-verification: ON
455  *
456  * Local Variables:
457  * eval: (c-set-style "gnu")
458  * End:
459  */