[aarch64] Fixes CLI crashes on dpaa2 platform.
[vpp.git] / src / vnet / devices / af_packet / node.c
1 /*------------------------------------------------------------------
2  * af_packet.c - linux kernel packet interface
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *------------------------------------------------------------------
17  */
18
19 #include <linux/if_packet.h>
20 #include <linux/virtio_net.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/devices/devices.h>
27 #include <vnet/feature/feature.h>
28
29 #include <vnet/devices/af_packet/af_packet.h>
30
31 #define foreach_af_packet_input_error
32
33 typedef enum
34 {
35 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
36   foreach_af_packet_input_error
37 #undef _
38     AF_PACKET_INPUT_N_ERROR,
39 } af_packet_input_error_t;
40
41 static char *af_packet_input_error_strings[] = {
42 #define _(n,s) s,
43   foreach_af_packet_input_error
44 #undef _
45 };
46
47 typedef struct
48 {
49   u32 next_index;
50   u32 hw_if_index;
51   int block;
52   struct tpacket2_hdr tph;
53 } af_packet_input_trace_t;
54
55 static u8 *
56 format_af_packet_input_trace (u8 * s, va_list * args)
57 {
58   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60   af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
61   u32 indent = format_get_indent (s);
62
63   s = format (s, "af_packet: hw_if_index %d next-index %d",
64               t->hw_if_index, t->next_index);
65
66   s =
67     format (s,
68             "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
69             "\n%Usec 0x%x nsec 0x%x vlan %U"
70 #ifdef TP_STATUS_VLAN_TPID_VALID
71             " vlan_tpid %u"
72 #endif
73             ,
74             format_white_space, indent + 2,
75             format_white_space, indent + 4,
76             t->tph.tp_status,
77             t->tph.tp_len,
78             t->tph.tp_snaplen,
79             t->tph.tp_mac,
80             t->tph.tp_net,
81             format_white_space, indent + 4,
82             t->tph.tp_sec,
83             t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
84 #ifdef TP_STATUS_VLAN_TPID_VALID
85             , t->tph.tp_vlan_tpid
86 #endif
87     );
88   return s;
89 }
90
91 always_inline void
92 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
93 {
94   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
95   vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
96   vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
97
98   /* update first buffer */
99   first_b->total_length_not_including_first_buffer += b->current_length;
100
101   /* update previous buffer */
102   prev_b->next_buffer = bi;
103   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
104
105   /* update current buffer */
106   b->next_buffer = 0;
107 }
108
109 always_inline uword
110 af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
111                            vlib_frame_t * frame, af_packet_if_t * apif)
112 {
113   af_packet_main_t *apm = &af_packet_main;
114   struct tpacket2_hdr *tph;
115   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
116   u32 block = 0;
117   u32 rx_frame;
118   u32 n_free_bufs;
119   u32 n_rx_packets = 0;
120   u32 n_rx_bytes = 0;
121   u32 *to_next = 0;
122   u32 block_size = apif->rx_req->tp_block_size;
123   u32 frame_size = apif->rx_req->tp_frame_size;
124   u32 frame_num = apif->rx_req->tp_frame_nr;
125   u8 *block_start = apif->rx_ring + block * block_size;
126   uword n_trace = vlib_get_trace_count (vm, node);
127   u32 thread_index = vlib_get_thread_index ();
128   u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
129                                                           VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
130   u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
131
132   if (apif->per_interface_next_index != ~0)
133     next_index = apif->per_interface_next_index;
134
135   n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
136   if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
137     {
138       vec_validate (apm->rx_buffers[thread_index],
139                     VLIB_FRAME_SIZE + n_free_bufs - 1);
140       n_free_bufs +=
141         vlib_buffer_alloc (vm, &apm->rx_buffers[thread_index][n_free_bufs],
142                            VLIB_FRAME_SIZE);
143       _vec_len (apm->rx_buffers[thread_index]) = n_free_bufs;
144     }
145
146   rx_frame = apif->next_rx_frame;
147   tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
148   while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
149     {
150       vlib_buffer_t *b0 = 0, *first_b0 = 0;
151       u32 next0 = next_index;
152
153       u32 n_left_to_next;
154       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
155       while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
156              n_left_to_next)
157         {
158
159           struct virtio_net_hdr *vh =
160             (struct virtio_net_hdr *) (((u8 *) tph) + tph->tp_mac -
161                                        sizeof (struct virtio_net_hdr));
162           u32 data_len = tph->tp_snaplen;
163           u32 offset = 0;
164           u32 bi0 = 0, first_bi0 = 0, prev_bi0;
165           u32 vlan_len = 0;
166           ip_csum_t wsum = 0;
167           u16 *wsum_addr = NULL;
168           u32 do_vnet = apm->flags & AF_PACKET_USES_VNET_HEADERS;
169           u32 do_csum = tph->tp_status & TP_STATUS_CSUMNOTREADY;
170
171           while (data_len)
172             {
173               /* grab free buffer */
174               u32 last_empty_buffer =
175                 vec_len (apm->rx_buffers[thread_index]) - 1;
176               prev_bi0 = bi0;
177               bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
178               b0 = vlib_get_buffer (vm, bi0);
179               _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer;
180               n_free_bufs--;
181
182               /* copy data */
183               u32 bytes_to_copy =
184                 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
185               u32 bytes_copied = 0;
186               b0->current_data = 0;
187               /* Kernel removes VLAN headers, so reconstruct VLAN */
188               if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
189                 {
190                   if (PREDICT_TRUE (offset == 0))
191                     {
192                       clib_memcpy (vlib_buffer_get_current (b0),
193                                    (u8 *) tph + tph->tp_mac,
194                                    sizeof (ethernet_header_t));
195                       ethernet_header_t *eth = vlib_buffer_get_current (b0);
196                       ethernet_vlan_header_t *vlan =
197                         (ethernet_vlan_header_t *) (eth + 1);
198                       vlan->priority_cfi_and_id =
199                         clib_host_to_net_u16 (tph->tp_vlan_tci);
200                       vlan->type = eth->type;
201                       eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
202                       vlan_len = sizeof (ethernet_vlan_header_t);
203                       bytes_copied = sizeof (ethernet_header_t);
204                     }
205                 }
206               /* Check if the incoming skb is marked as CSUM_PARTIAL,
207                * If VNET Headers are enabled TP_STATUS_CSUMNOTREADY is
208                * equivalent to the vnet csum flag.
209                **/
210               if (PREDICT_TRUE ((do_vnet != 0) && (do_csum != 0)))
211                 {
212                   wsum_addr = (u16 *) (((u8 *) vlib_buffer_get_current (b0)) +
213                                        vlan_len + vh->csum_start +
214                                        vh->csum_offset);
215                   if (bytes_copied <= vh->csum_start)
216                     {
217                       clib_memcpy (((u8 *) vlib_buffer_get_current (b0)) +
218                                    bytes_copied + vlan_len,
219                                    (u8 *) tph + tph->tp_mac + offset +
220                                    bytes_copied,
221                                    (vh->csum_start - bytes_copied));
222                       wsum =
223                         ip_csum_and_memcpy (wsum,
224                                             ((u8 *)
225                                              vlib_buffer_get_current (b0)) +
226                                             vh->csum_start + vlan_len,
227                                             (u8 *) tph + tph->tp_mac +
228                                             offset + vh->csum_start,
229                                             (bytes_to_copy - vh->csum_start));
230                     }
231                   else
232                     {
233                       wsum =
234                         ip_csum_and_memcpy (wsum,
235                                             ((u8 *)
236                                              vlib_buffer_get_current (b0)) +
237                                             bytes_copied + vlan_len,
238                                             (u8 *) tph + tph->tp_mac +
239                                             offset + bytes_copied,
240                                             (bytes_to_copy - bytes_copied));
241                     }
242                 }
243               else
244                 {
245                   clib_memcpy (((u8 *) vlib_buffer_get_current (b0)) +
246                                bytes_copied + vlan_len,
247                                (u8 *) tph + tph->tp_mac + offset +
248                                bytes_copied, (bytes_to_copy - bytes_copied));
249                 }
250
251               /* fill buffer header */
252               b0->current_length = bytes_to_copy + vlan_len;
253
254               if (offset == 0)
255                 {
256                   b0->total_length_not_including_first_buffer = 0;
257                   b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
258                   vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
259                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
260                   first_bi0 = bi0;
261                   first_b0 = vlib_get_buffer (vm, first_bi0);
262                 }
263               else
264                 buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
265
266               offset += bytes_to_copy;
267               data_len -= bytes_to_copy;
268             }
269           if (PREDICT_TRUE ((do_vnet != 0) && (do_csum != 0)))
270             {
271               *wsum_addr = ~ip_csum_fold (wsum);
272             }
273           n_rx_packets++;
274           n_rx_bytes += tph->tp_snaplen;
275           to_next[0] = first_bi0;
276           to_next += 1;
277           n_left_to_next--;
278
279           /* trace */
280           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
281           if (PREDICT_FALSE (n_trace > 0))
282             {
283               af_packet_input_trace_t *tr;
284               vlib_trace_buffer (vm, node, next0, first_b0,     /* follow_chain */
285                                  0);
286               vlib_set_trace_count (vm, node, --n_trace);
287               tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
288               tr->next_index = next0;
289               tr->hw_if_index = apif->hw_if_index;
290               clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr));
291             }
292
293           /* redirect if feature path enabled */
294           vnet_feature_start_device_input_x1 (apif->sw_if_index, &next0, b0);
295
296           /* enque and take next packet */
297           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
298                                            n_left_to_next, first_bi0, next0);
299
300           /* next packet */
301           tph->tp_status = TP_STATUS_KERNEL;
302           rx_frame = (rx_frame + 1) % frame_num;
303           tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
304         }
305
306       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307     }
308
309   apif->next_rx_frame = rx_frame;
310
311   vlib_increment_combined_counter
312     (vnet_get_main ()->interface_main.combined_sw_if_counters
313      + VNET_INTERFACE_COUNTER_RX,
314      vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
315
316   vnet_device_increment_rx_packets (thread_index, n_rx_packets);
317   return n_rx_packets;
318 }
319
320 static uword
321 af_packet_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
322                     vlib_frame_t * frame)
323 {
324   u32 n_rx_packets = 0;
325   af_packet_main_t *apm = &af_packet_main;
326   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
327   vnet_device_and_queue_t *dq;
328
329   foreach_device_and_queue (dq, rt->devices_and_queues)
330   {
331     af_packet_if_t *apif;
332     apif = vec_elt_at_index (apm->interfaces, dq->dev_instance);
333     if (apif->is_admin_up)
334       n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif);
335   }
336
337   return n_rx_packets;
338 }
339
340 /* *INDENT-OFF* */
341 VLIB_REGISTER_NODE (af_packet_input_node) = {
342   .function = af_packet_input_fn,
343   .name = "af-packet-input",
344   .sibling_of = "device-input",
345   .format_trace = format_af_packet_input_trace,
346   .type = VLIB_NODE_TYPE_INPUT,
347   .state = VLIB_NODE_STATE_INTERRUPT,
348   .n_errors = AF_PACKET_INPUT_N_ERROR,
349   .error_strings = af_packet_input_error_strings,
350 };
351
352 VLIB_NODE_FUNCTION_MULTIARCH (af_packet_input_node, af_packet_input_fn)
353 /* *INDENT-ON* */
354
355
356 /*
357  * fd.io coding-style-patch-verification: ON
358  *
359  * Local Variables:
360  * eval: (c-set-style "gnu")
361  * End:
362  */