Coding standards cleanup in vnet/vnet/devices, fixes VPP-248
[vpp.git] / vnet / vnet / devices / netmap / node.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <stdint.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25
26 #include <vnet/devices/netmap/net_netmap.h>
27 #include <vnet/devices/netmap/netmap.h>
28
29 #define foreach_netmap_input_error
30
31 typedef enum
32 {
33 #define _(f,s) NETMAP_INPUT_ERROR_##f,
34   foreach_netmap_input_error
35 #undef _
36     NETMAP_INPUT_N_ERROR,
37 } netmap_input_error_t;
38
39 static char *netmap_input_error_strings[] = {
40 #define _(n,s) s,
41   foreach_netmap_input_error
42 #undef _
43 };
44
45 enum
46 {
47   NETMAP_INPUT_NEXT_DROP,
48   NETMAP_INPUT_NEXT_ETHERNET_INPUT,
49   NETMAP_INPUT_N_NEXT,
50 };
51
52 typedef struct
53 {
54   u32 next_index;
55   u32 hw_if_index;
56   struct netmap_slot slot;
57 } netmap_input_trace_t;
58
59 static u8 *
60 format_netmap_input_trace (u8 * s, va_list * args)
61 {
62   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64   netmap_input_trace_t *t = va_arg (*args, netmap_input_trace_t *);
65   uword indent = format_get_indent (s);
66
67   s = format (s, "netmap: hw_if_index %d next-index %d",
68               t->hw_if_index, t->next_index);
69   s = format (s, "\n%Uslot: flags 0x%x len %u buf_idx %u",
70               format_white_space, indent + 2,
71               t->slot.flags, t->slot.len, t->slot.buf_idx);
72   return s;
73 }
74
75 always_inline void
76 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
77 {
78   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
79   vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
80   vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
81
82   /* update first buffer */
83   first_b->total_length_not_including_first_buffer += b->current_length;
84
85   /* update previous buffer */
86   prev_b->next_buffer = bi;
87   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
88
89   /* update current buffer */
90   b->next_buffer = 0;
91
92 #if DPDK > 0
93   struct rte_mbuf *mbuf = rte_mbuf_from_vlib_buffer (b);
94   struct rte_mbuf *first_mbuf = rte_mbuf_from_vlib_buffer (first_b);
95   struct rte_mbuf *prev_mbuf = rte_mbuf_from_vlib_buffer (prev_b);
96   first_mbuf->nb_segs++;
97   prev_mbuf->next = mbuf;
98   mbuf->data_len = b->current_length;
99   mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
100   mbuf->next = 0;
101 #endif
102 }
103
104 always_inline uword
105 netmap_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
106                         vlib_frame_t * frame, netmap_if_t * nif)
107 {
108   u32 next_index = NETMAP_INPUT_NEXT_ETHERNET_INPUT;
109   uword n_trace = vlib_get_trace_count (vm, node);
110   netmap_main_t *nm = &netmap_main;
111   u32 n_rx_packets = 0;
112   u32 n_rx_bytes = 0;
113   u32 *to_next = 0;
114   u32 n_free_bufs;
115   struct netmap_ring *ring;
116   int cur_ring;
117   u32 cpu_index = os_get_cpu_number ();
118   u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
119                                                           VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
120
121   if (nif->per_interface_next_index != ~0)
122     next_index = nif->per_interface_next_index;
123
124   n_free_bufs = vec_len (nm->rx_buffers[cpu_index]);
125   if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
126     {
127       vec_validate (nm->rx_buffers[cpu_index],
128                     VLIB_FRAME_SIZE + n_free_bufs - 1);
129       n_free_bufs +=
130         vlib_buffer_alloc (vm, &nm->rx_buffers[cpu_index][n_free_bufs],
131                            VLIB_FRAME_SIZE);
132       _vec_len (nm->rx_buffers[cpu_index]) = n_free_bufs;
133     }
134
135   cur_ring = nif->first_rx_ring;
136   while (cur_ring <= nif->last_rx_ring && n_free_bufs)
137     {
138       int r = 0;
139       u32 cur_slot_index;
140       ring = NETMAP_RXRING (nif->nifp, cur_ring);
141       r = nm_ring_space (ring);
142
143       if (!r)
144         {
145           cur_ring++;
146           continue;
147         }
148
149       if (r > n_free_bufs)
150         r = n_free_bufs;
151
152       cur_slot_index = ring->cur;
153       while (r)
154         {
155           u32 n_left_to_next;
156           u32 next0 = next_index;
157           vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
158
159           while (r && n_left_to_next)
160             {
161               vlib_buffer_t *b0, *first_b0 = 0;
162               u32 offset = 0;
163               u32 bi0 = 0, first_bi0 = 0, prev_bi0;
164               u32 next_slot_index = (cur_slot_index + 1) % ring->num_slots;
165               u32 next2_slot_index = (cur_slot_index + 2) % ring->num_slots;
166               struct netmap_slot *slot = &ring->slot[cur_slot_index];
167               u32 data_len = slot->len;
168
169               /* prefetch 2 slots in advance */
170               CLIB_PREFETCH (&ring->slot[next2_slot_index],
171                              CLIB_CACHE_LINE_BYTES, LOAD);
172               /* prefetch start of next packet */
173               CLIB_PREFETCH (NETMAP_BUF
174                              (ring, ring->slot[next_slot_index].buf_idx),
175                              CLIB_CACHE_LINE_BYTES, LOAD);
176
177               while (data_len && n_free_bufs)
178                 {
179                   /* grab free buffer */
180                   u32 last_empty_buffer =
181                     vec_len (nm->rx_buffers[cpu_index]) - 1;
182                   prev_bi0 = bi0;
183                   bi0 = nm->rx_buffers[cpu_index][last_empty_buffer];
184                   b0 = vlib_get_buffer (vm, bi0);
185                   _vec_len (nm->rx_buffers[cpu_index]) = last_empty_buffer;
186                   n_free_bufs--;
187
188                   /* copy data */
189                   u32 bytes_to_copy =
190                     data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
191                   b0->current_data = 0;
192                   clib_memcpy (vlib_buffer_get_current (b0),
193                                (u8 *) NETMAP_BUF (ring,
194                                                   slot->buf_idx) + offset,
195                                bytes_to_copy);
196
197                   /* fill buffer header */
198                   b0->current_length = bytes_to_copy;
199
200                   if (offset == 0)
201                     {
202 #if DPDK > 0
203                       struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer (b0);
204                       rte_pktmbuf_data_len (mb) = b0->current_length;
205                       rte_pktmbuf_pkt_len (mb) = b0->current_length;
206 #endif
207                       b0->total_length_not_including_first_buffer = 0;
208                       b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
209                       vnet_buffer (b0)->sw_if_index[VLIB_RX] =
210                         nif->sw_if_index;
211                       vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
212                       first_bi0 = bi0;
213                       first_b0 = vlib_get_buffer (vm, first_bi0);
214                     }
215                   else
216                     buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
217
218                   offset += bytes_to_copy;
219                   data_len -= bytes_to_copy;
220                 }
221
222               /* trace */
223               VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
224               if (PREDICT_FALSE (n_trace > 0))
225                 {
226                   if (PREDICT_TRUE (first_b0 != 0))
227                     {
228                       netmap_input_trace_t *tr;
229                       vlib_trace_buffer (vm, node, next0, first_b0,
230                                          /* follow_chain */ 0);
231                       vlib_set_trace_count (vm, node, --n_trace);
232                       tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
233                       tr->next_index = next0;
234                       tr->hw_if_index = nif->hw_if_index;
235                       memcpy (&tr->slot, slot, sizeof (struct netmap_slot));
236                     }
237                 }
238               /* enque and take next packet */
239               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
240                                                n_left_to_next, first_bi0,
241                                                next0);
242
243               /* next packet */
244               n_rx_packets++;
245               n_rx_bytes += slot->len;
246               to_next[0] = first_bi0;
247               to_next += 1;
248               n_left_to_next--;
249               cur_slot_index = next_slot_index;
250
251               r--;
252             }
253           vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254         }
255       ring->head = ring->cur = cur_slot_index;
256       cur_ring++;
257     }
258
259   if (n_rx_packets)
260     ioctl (nif->fd, NIOCRXSYNC, NULL);
261
262   vlib_increment_combined_counter
263     (vnet_get_main ()->interface_main.combined_sw_if_counters
264      + VNET_INTERFACE_COUNTER_RX,
265      os_get_cpu_number (), nif->hw_if_index, n_rx_packets, n_rx_bytes);
266
267   return n_rx_packets;
268 }
269
270 static uword
271 netmap_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
272                  vlib_frame_t * frame)
273 {
274   int i;
275   u32 n_rx_packets = 0;
276   u32 cpu_index = os_get_cpu_number ();
277   netmap_main_t *nm = &netmap_main;
278   netmap_if_t *nmi;
279
280   for (i = 0; i < vec_len (nm->interfaces); i++)
281     {
282       nmi = vec_elt_at_index (nm->interfaces, i);
283       if (nmi->is_admin_up &&
284           (i % nm->input_cpu_count) ==
285           (cpu_index - nm->input_cpu_first_index))
286         n_rx_packets += netmap_device_input_fn (vm, node, frame, nmi);
287     }
288
289   return n_rx_packets;
290 }
291
292 /* *INDENT-OFF* */
293 VLIB_REGISTER_NODE (netmap_input_node) = {
294   .function = netmap_input_fn,
295   .name = "netmap-input",
296   .format_trace = format_netmap_input_trace,
297   .type = VLIB_NODE_TYPE_INPUT,
298   /* default state is INTERRUPT mode, switch to POLLING if worker threads are enabled */
299   .state = VLIB_NODE_STATE_INTERRUPT,
300   .n_errors = NETMAP_INPUT_N_ERROR,
301   .error_strings = netmap_input_error_strings,
302
303   .n_next_nodes = NETMAP_INPUT_N_NEXT,
304   .next_nodes = {
305     [NETMAP_INPUT_NEXT_DROP] = "error-drop",
306     [NETMAP_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",
307   },
308 };
309
310 VLIB_NODE_FUNCTION_MULTIARCH (netmap_input_node, netmap_input_fn)
311 /* *INDENT-ON* */
312
313
314 /*
315  * fd.io coding-style-patch-verification: ON
316  *
317  * Local Variables:
318  * eval: (c-set-style "gnu")
319  * End:
320  */