af_packet: multithreading support
[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
23 #include <vlib/vlib.h>
24 #include <vlib/unix/unix.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ethernet/ethernet.h>
27
28 #include <vnet/devices/af_packet/af_packet.h>
29
30 #define AF_PACKET_DEBUG_SOCKET          0
31
32 #define AF_PACKET_TX_FRAMES_PER_BLOCK   1024
33 #define AF_PACKET_TX_FRAME_SIZE         (2048 * 5)
34 #define AF_PACKET_TX_BLOCK_NR           1
35 #define AF_PACKET_TX_FRAME_NR           (AF_PACKET_TX_BLOCK_NR * \
36                                          AF_PACKET_TX_FRAMES_PER_BLOCK)
37 #define AF_PACKET_TX_BLOCK_SIZE         (AF_PACKET_TX_FRAME_SIZE * \
38                                          AF_PACKET_TX_FRAMES_PER_BLOCK)
39
40 #define AF_PACKET_RX_FRAMES_PER_BLOCK   1024
41 #define AF_PACKET_RX_FRAME_SIZE         (2048 * 5)
42 #define AF_PACKET_RX_BLOCK_NR           1
43 #define AF_PACKET_RX_FRAME_NR           (AF_PACKET_RX_BLOCK_NR * \
44                                          AF_PACKET_RX_FRAMES_PER_BLOCK)
45 #define AF_PACKET_RX_BLOCK_SIZE         (AF_PACKET_RX_FRAME_SIZE * \
46                                          AF_PACKET_RX_FRAMES_PER_BLOCK)
47
48 #if AF_PACKET_DEBUG_SOCKET == 1
49 #define DBG_SOCK(args...) clib_warning(args);
50 #else
51 #define DBG_SOCK(args...)
52 #endif
53
54 /*defined in net/if.h but clashes with dpdk headers */
55 unsigned int if_nametoindex (const char *ifname);
56
57 typedef struct tpacket_req tpacket_req_t;
58
59 static u32
60 af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
61                            u32 flags)
62 {
63   /* nothing for now */
64   return 0;
65 }
66
67 static clib_error_t *
68 af_packet_fd_read_ready (unix_file_t * uf)
69 {
70   vlib_main_t *vm = vlib_get_main ();
71   af_packet_main_t *apm = &af_packet_main;
72   u32 idx = uf->private_data;
73
74   apm->pending_input_bitmap =
75     clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
76
77   /* Schedule the rx node */
78   vlib_node_set_interrupt_pending (vm, af_packet_input_node.index);
79
80   return 0;
81 }
82
83 static int
84 create_packet_v2_sock (u8 * name, tpacket_req_t * rx_req,
85                        tpacket_req_t * tx_req, int *fd, u8 ** ring)
86 {
87   int ret, err;
88   struct sockaddr_ll sll;
89   uint host_if_index;
90   int ver = TPACKET_V2;
91   socklen_t req_sz = sizeof (struct tpacket_req);
92   u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
93     tx_req->tp_block_size * tx_req->tp_block_nr;
94
95   host_if_index = if_nametoindex ((const char *) name);
96
97   if (!host_if_index)
98     {
99       DBG_SOCK ("Wrong host interface name");
100       ret = VNET_API_ERROR_INVALID_INTERFACE;
101       goto error;
102     }
103
104   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
105     {
106       DBG_SOCK ("Failed to create socket");
107       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
108       goto error;
109     }
110
111   if ((err =
112        setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
113     {
114       DBG_SOCK ("Failed to set rx packet interface version");
115       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
116       goto error;
117     }
118
119   int opt = 1;
120   if ((err =
121        setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt))) < 0)
122     {
123       DBG_SOCK ("Failed to set packet tx ring error handling option");
124       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
125       goto error;
126     }
127
128   if ((err =
129        setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
130     {
131       DBG_SOCK ("Failed to set packet rx ring options");
132       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
133       goto error;
134     }
135
136   if ((err =
137        setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
138     {
139       DBG_SOCK ("Failed to set packet rx ring options");
140       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
141       goto error;
142     }
143
144   *ring =
145     mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
146           0);
147   if (*ring == MAP_FAILED)
148     {
149       DBG_SOCK ("mmap failure");
150       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
151       goto error;
152     }
153
154   memset (&sll, 0, sizeof (sll));
155   sll.sll_family = PF_PACKET;
156   sll.sll_protocol = htons (ETH_P_ALL);
157   sll.sll_ifindex = host_if_index;
158
159   if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
160     {
161       DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
162       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
163       goto error;
164     }
165
166   return 0;
167 error:
168   if (*fd >= 0)
169     close (*fd);
170   *fd = -1;
171   return ret;
172 }
173
174 static void
175 af_packet_worker_thread_enable ()
176 {
177   /* If worker threads are enabled, switch to polling mode */
178   foreach_vlib_main ((
179                        {
180                        vlib_node_set_state (this_vlib_main,
181                                             af_packet_input_node.index,
182                                             VLIB_NODE_STATE_POLLING);
183                        }));
184
185 }
186
187 static void
188 af_packet_worker_thread_disable ()
189 {
190   foreach_vlib_main ((
191                        {
192                        vlib_node_set_state (this_vlib_main,
193                                             af_packet_input_node.index,
194                                             VLIB_NODE_STATE_INTERRUPT);
195                        }));
196
197 }
198
199 int
200 af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
201                      u32 * sw_if_index)
202 {
203   af_packet_main_t *apm = &af_packet_main;
204   int ret, fd = -1;
205   struct tpacket_req *rx_req = 0;
206   struct tpacket_req *tx_req = 0;
207   u8 *ring = 0;
208   af_packet_if_t *apif = 0;
209   u8 hw_addr[6];
210   clib_error_t *error;
211   vnet_sw_interface_t *sw;
212   vlib_thread_main_t *tm = vlib_get_thread_main ();
213   vnet_main_t *vnm = vnet_get_main ();
214   uword *p;
215   uword if_index;
216   u8 *host_if_name_dup = vec_dup (host_if_name);
217
218   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
219   if (p)
220     {
221       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
222     }
223
224   vec_validate (rx_req, 0);
225   rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
226   rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
227   rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
228   rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR;
229
230   vec_validate (tx_req, 0);
231   tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE;
232   tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE;
233   tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
234   tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
235
236   ret = create_packet_v2_sock (host_if_name, rx_req, tx_req, &fd, &ring);
237
238   if (ret != 0)
239     goto error;
240
241   /* So far everything looks good, let's create interface */
242   pool_get (apm->interfaces, apif);
243   if_index = apif - apm->interfaces;
244
245   apif->fd = fd;
246   apif->rx_ring = ring;
247   apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
248   apif->rx_req = rx_req;
249   apif->tx_req = tx_req;
250   apif->host_if_name = host_if_name_dup;
251   apif->per_interface_next_index = ~0;
252   apif->next_tx_frame = 0;
253   apif->next_rx_frame = 0;
254
255   if (tm->n_vlib_mains > 1)
256     {
257       apif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
258                                             CLIB_CACHE_LINE_BYTES);
259       memset ((void *) apif->lockp, 0, CLIB_CACHE_LINE_BYTES);
260     }
261
262   {
263     unix_file_t template = { 0 };
264     template.read_function = af_packet_fd_read_ready;
265     template.file_descriptor = fd;
266     template.private_data = if_index;
267     template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
268     apif->unix_file_index = unix_file_add (&unix_main, &template);
269   }
270
271   /*use configured or generate random MAC address */
272   if (hw_addr_set)
273     clib_memcpy (hw_addr, hw_addr_set, 6);
274   else
275     {
276       f64 now = vlib_time_now (vm);
277       u32 rnd;
278       rnd = (u32) (now * 1e6);
279       rnd = random_u32 (&rnd);
280
281       clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
282       hw_addr[0] = 2;
283       hw_addr[1] = 0xfe;
284     }
285
286   error = ethernet_register_interface (vnm, af_packet_device_class.index,
287                                        if_index, hw_addr, &apif->hw_if_index,
288                                        af_packet_eth_flag_change);
289
290   if (error)
291     {
292       memset (apif, 0, sizeof (*apif));
293       pool_put (apm->interfaces, apif);
294       clib_error_report (error);
295       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
296       goto error;
297     }
298
299   sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
300   apif->sw_if_index = sw->sw_if_index;
301
302   vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
303                                VNET_HW_INTERFACE_FLAG_LINK_UP);
304
305   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
306                  0);
307   if (sw_if_index)
308     *sw_if_index = apif->sw_if_index;
309
310   if (tm->n_vlib_mains > 1 && pool_elts (apm->interfaces) == 1)
311     af_packet_worker_thread_enable ();
312
313   return 0;
314
315 error:
316   vec_free (host_if_name_dup);
317   vec_free (rx_req);
318   vec_free (tx_req);
319   return ret;
320 }
321
322 int
323 af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
324 {
325   vnet_main_t *vnm = vnet_get_main ();
326   vlib_thread_main_t *tm = vlib_get_thread_main ();
327   af_packet_main_t *apm = &af_packet_main;
328   af_packet_if_t *apif;
329   uword *p;
330   uword if_index;
331   u32 ring_sz;
332
333   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
334   if (p == NULL)
335     {
336       clib_warning ("Host interface %s does not exist", host_if_name);
337       return VNET_API_ERROR_SYSCALL_ERROR_1;
338     }
339   apif = pool_elt_at_index (apm->interfaces, p[0]);
340   if_index = apif - apm->interfaces;
341
342   /* bring down the interface */
343   vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
344
345   /* clean up */
346   if (apif->unix_file_index != ~0)
347     {
348       unix_file_del (&unix_main, unix_main.file_pool + apif->unix_file_index);
349       apif->unix_file_index = ~0;
350     }
351   else
352     close (apif->fd);
353
354   ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
355     apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
356   if (munmap (apif->rx_ring, ring_sz))
357     clib_warning ("Host interface %s could not free rx/tx ring",
358                   host_if_name);
359   apif->rx_ring = NULL;
360   apif->tx_ring = NULL;
361   apif->fd = -1;
362
363   vec_free (apif->rx_req);
364   apif->rx_req = NULL;
365   vec_free (apif->tx_req);
366   apif->tx_req = NULL;
367
368   vec_free (apif->host_if_name);
369   apif->host_if_name = NULL;
370
371   mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);
372
373   ethernet_delete_interface (vnm, apif->hw_if_index);
374
375   pool_put (apm->interfaces, apif);
376   if (tm->n_vlib_mains > 1 && pool_elts (apm->interfaces) == 0)
377     af_packet_worker_thread_disable ();
378
379   return 0;
380 }
381
382 static clib_error_t *
383 af_packet_init (vlib_main_t * vm)
384 {
385   af_packet_main_t *apm = &af_packet_main;
386   vlib_thread_main_t *tm = vlib_get_thread_main ();
387   vlib_thread_registration_t *tr;
388   uword *p;
389
390   memset (apm, 0, sizeof (af_packet_main_t));
391
392   apm->input_cpu_first_index = 0;
393   apm->input_cpu_count = 1;
394
395   /* find out which cpus will be used for input */
396   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
397   tr = p ? (vlib_thread_registration_t *) p[0] : 0;
398
399   if (tr && tr->count > 0)
400     {
401       apm->input_cpu_first_index = tr->first_index;
402       apm->input_cpu_count = tr->count;
403     }
404
405   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
406
407   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
408                         CLIB_CACHE_LINE_BYTES);
409
410   return 0;
411 }
412
413 VLIB_INIT_FUNCTION (af_packet_init);
414
415 /*
416  * fd.io coding-style-patch-verification: ON
417  *
418  * Local Variables:
419  * eval: (c-set-style "gnu")
420  * End:
421  */