15a96f442f92e2f4b97352d828fa19e4a86423ed
[vpp.git] / vnet / vnet / devices / af_packet / node.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_packet.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
27 #include <vnet/devices/af_packet/af_packet.h>
28
29 #define foreach_af_packet_input_error
30
31 typedef enum {
32 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
33   foreach_af_packet_input_error
34 #undef _
35   AF_PACKET_INPUT_N_ERROR,
36 } af_packet_input_error_t;
37
38 static char * af_packet_input_error_strings[] = {
39 #define _(n,s) s,
40     foreach_af_packet_input_error
41 #undef _
42 };
43
44 enum {
45   AF_PACKET_INPUT_NEXT_DROP,
46   AF_PACKET_INPUT_NEXT_ETHERNET_INPUT,
47   AF_PACKET_INPUT_N_NEXT,
48 };
49
50 typedef struct {
51   u32 next_index;
52   u32 hw_if_index;
53   int block;
54   struct tpacket2_hdr tph;
55 } af_packet_input_trace_t;
56
57 static u8 * format_af_packet_input_trace (u8 * s, va_list * args)
58 {
59   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61   af_packet_input_trace_t * t = va_arg (*args, af_packet_input_trace_t *);
62   uword indent = format_get_indent (s);
63
64   s = format (s, "af_packet: hw_if_index %d next-index %d",
65               t->hw_if_index, t->next_index);
66
67   s = format (s, "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
68               "\n%Usec 0x%x nsec 0x%x vlan_tci %u"
69 #ifdef TP_STATUS_VLAN_TPID_VALID
70               " vlan_tpid %u"
71 #endif
72               ,
73               format_white_space, indent + 2,
74               format_white_space, indent + 4,
75               t->tph.tp_status,
76               t->tph.tp_len,
77               t->tph.tp_snaplen,
78               t->tph.tp_mac,
79               t->tph.tp_net,
80               format_white_space, indent + 4,
81               t->tph.tp_sec,
82               t->tph.tp_nsec,
83               t->tph.tp_vlan_tci,
84 #ifdef TP_STATUS_VLAN_TPID_VALID
85               t->tph.tp_vlan_tpid,
86 #endif
87               t->tph.tp_net);
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 #if DPDK > 0
98   struct rte_mbuf * mbuf = ((struct rte_mbuf *) b) - 1;
99   struct rte_mbuf * first_mbuf = ((struct rte_mbuf *) first_b) - 1;
100   struct rte_mbuf * prev_mbuf = ((struct rte_mbuf *) prev_b) - 1;
101 #endif
102
103   /* update first buffer */
104   first_b->total_length_not_including_first_buffer +=  b->current_length;
105
106   /* update previous buffer */
107   prev_b->next_buffer = bi;
108   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
109
110   /* update current buffer */
111   b->next_buffer = 0;
112
113 #if DPDK > 0
114   first_mbuf->nb_segs++;
115   prev_mbuf->next = mbuf;
116   mbuf->data_len = b->current_length;
117   mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
118   mbuf->next = 0;
119 #endif
120 }
121
122 always_inline uword
123 af_packet_device_input_fn  (vlib_main_t * vm, vlib_node_runtime_t * node,
124                             vlib_frame_t * frame, u32 device_idx)
125 {
126   af_packet_main_t * apm = &af_packet_main;
127   af_packet_if_t * apif = pool_elt_at_index(apm->interfaces, device_idx);
128   struct tpacket2_hdr *tph;
129   u32 next_index = AF_PACKET_INPUT_NEXT_ETHERNET_INPUT;
130   u32 block = 0;
131   u32 rx_frame;
132   u32 n_free_bufs;
133   u32 n_rx_packets = 0;
134   u32 n_rx_bytes = 0;
135   u32 * to_next = 0;
136   u32 block_size = apif->rx_req->tp_block_size;
137   u32 frame_size = apif->rx_req->tp_frame_size;
138   u32 frame_num = apif->rx_req->tp_frame_nr;
139   u8 * block_start = apif->rx_ring + block * block_size;
140   uword n_trace = vlib_get_trace_count (vm, node);
141   u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
142     VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
143   u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
144
145   n_free_bufs = vec_len (apm->rx_buffers);
146   if (PREDICT_FALSE(n_free_bufs < VLIB_FRAME_SIZE))
147     {
148       vec_validate(apm->rx_buffers, VLIB_FRAME_SIZE + n_free_bufs - 1);
149       n_free_bufs += vlib_buffer_alloc(vm, &apm->rx_buffers[n_free_bufs], VLIB_FRAME_SIZE);
150       _vec_len (apm->rx_buffers) = n_free_bufs;
151     }
152
153   rx_frame = apif->next_rx_frame;
154   tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
155   while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
156     {
157       vlib_buffer_t * b0, * first_b0 = 0;
158       u32 next0 = AF_PACKET_INPUT_NEXT_ETHERNET_INPUT;
159
160       u32 n_left_to_next;
161       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
162       while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
163              n_left_to_next)
164         {
165           u32 data_len = tph->tp_snaplen;
166           u32 offset = 0;
167           u32 bi0, first_bi0 = 0, prev_bi0;
168
169           while (data_len)
170             {
171               /* grab free buffer */
172               u32 last_empty_buffer = vec_len (apm->rx_buffers) - 1;
173               prev_bi0 = bi0;
174               bi0 = apm->rx_buffers[last_empty_buffer];
175               b0 = vlib_get_buffer (vm, bi0);
176               _vec_len (apm->rx_buffers) = last_empty_buffer;
177               n_free_bufs--;
178
179               /* copy data */
180               u32 bytes_to_copy = data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
181               b0->current_data = 0;
182               memcpy (vlib_buffer_get_current (b0), (u8 *) tph + tph->tp_mac + offset, bytes_to_copy);
183
184               /* fill buffer header */
185               b0->clone_count = 0;
186               b0->current_length = bytes_to_copy;
187
188               if (offset == 0)
189                 {
190                   b0->total_length_not_including_first_buffer = 0;
191                   b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
192                   vnet_buffer(b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
193                   vnet_buffer(b0)->sw_if_index[VLIB_TX] = (u32)~0;
194                   first_bi0 = bi0;
195                   first_b0 = vlib_get_buffer(vm, first_bi0);
196                 }
197               else
198                 buffer_add_to_chain(vm, bi0, first_bi0, prev_bi0);
199
200               offset += bytes_to_copy;
201               data_len -= bytes_to_copy;
202             }
203           n_rx_packets++;
204           n_rx_bytes += tph->tp_snaplen;
205           to_next[0] = first_bi0;
206           to_next += 1;
207           n_left_to_next--;
208
209           /* trace */
210           VLIB_BUFFER_TRACE_TRAJECTORY_INIT(first_b0);
211           if (PREDICT_FALSE(n_trace > 0))
212             {
213               af_packet_input_trace_t *tr;
214               vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */ 0);
215               vlib_set_trace_count (vm, node, --n_trace);
216               tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
217               tr->next_index = next0;
218               tr->hw_if_index = apif->hw_if_index;
219               memcpy(&tr->tph, tph, sizeof(struct tpacket2_hdr));
220             }
221           /* enque and take next packet */
222           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
223                                            n_left_to_next, first_bi0, next0);
224
225            /* next packet */
226           tph->tp_status = TP_STATUS_KERNEL;
227           rx_frame = (rx_frame + 1) % frame_num;
228           tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
229         }
230
231       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
232     }
233
234   apif->next_rx_frame = rx_frame;
235
236   vlib_increment_combined_counter
237     (vnet_get_main()->interface_main.combined_sw_if_counters
238      + VNET_INTERFACE_COUNTER_RX,
239      os_get_cpu_number(),
240      apif->hw_if_index,
241      n_rx_packets, n_rx_bytes);
242
243   return n_rx_packets;
244 }
245
246 static uword
247 af_packet_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
248                     vlib_frame_t * frame)
249 {
250   int i;
251   u32 n_rx_packets = 0;
252
253   af_packet_main_t * apm = &af_packet_main;
254
255   clib_bitmap_foreach (i, apm->pending_input_bitmap,
256     ({
257       n_rx_packets += af_packet_device_input_fn(vm, node, frame, i);
258     }));
259
260   return n_rx_packets;
261 }
262
263
264 VLIB_REGISTER_NODE (af_packet_input_node) = {
265   .function = af_packet_input_fn,
266   .name = "af-packet-input",
267   .format_trace = format_af_packet_input_trace,
268   .type = VLIB_NODE_TYPE_INPUT,
269   .state = VLIB_NODE_STATE_INTERRUPT,
270   .n_errors = AF_PACKET_INPUT_N_ERROR,
271   .error_strings = af_packet_input_error_strings,
272
273   .n_next_nodes = AF_PACKET_INPUT_N_NEXT,
274   .next_nodes = {
275     [AF_PACKET_INPUT_NEXT_DROP] = "error-drop",
276     [AF_PACKET_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",
277   },
278 };