blob: 9293c0d811f32a05384917e0f8d02f539e2dbe60 [file] [log] [blame]
Damjan Marion108c7312016-04-20 05:04:20 +02001/*
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
Matus Fabian82e29c42016-05-11 04:49:46 -070018#include <stdint.h>
19#include <net/if.h>
20#include <sys/ioctl.h>
21
Damjan Marion108c7312016-04-20 05:04:20 +020022#include <vlib/vlib.h>
23#include <vlib/unix/unix.h>
24#include <vnet/ethernet/ethernet.h>
Damjan Marion8bdc63b2016-11-02 14:48:21 +010025#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020026#include <vnet/feature/feature.h>
Damjan Marion108c7312016-04-20 05:04:20 +020027
Matus Fabian82e29c42016-05-11 04:49:46 -070028#include <vnet/devices/netmap/net_netmap.h>
Damjan Marion108c7312016-04-20 05:04:20 +020029#include <vnet/devices/netmap/netmap.h>
30
31#define foreach_netmap_input_error
32
Damjan Marion00a9dca2016-08-17 17:05:46 +020033typedef enum
34{
Damjan Marion108c7312016-04-20 05:04:20 +020035#define _(f,s) NETMAP_INPUT_ERROR_##f,
36 foreach_netmap_input_error
37#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +020038 NETMAP_INPUT_N_ERROR,
Damjan Marion108c7312016-04-20 05:04:20 +020039} netmap_input_error_t;
40
Damjan Marion00a9dca2016-08-17 17:05:46 +020041static char *netmap_input_error_strings[] = {
Damjan Marion108c7312016-04-20 05:04:20 +020042#define _(n,s) s,
Damjan Marion00a9dca2016-08-17 17:05:46 +020043 foreach_netmap_input_error
Damjan Marion108c7312016-04-20 05:04:20 +020044#undef _
45};
46
Damjan Marion00a9dca2016-08-17 17:05:46 +020047typedef struct
48{
Damjan Marion108c7312016-04-20 05:04:20 +020049 u32 next_index;
50 u32 hw_if_index;
51 struct netmap_slot slot;
52} netmap_input_trace_t;
53
Damjan Marion00a9dca2016-08-17 17:05:46 +020054static u8 *
55format_netmap_input_trace (u8 * s, va_list * args)
Damjan Marion108c7312016-04-20 05:04:20 +020056{
57 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Damjan Marion00a9dca2016-08-17 17:05:46 +020059 netmap_input_trace_t *t = va_arg (*args, netmap_input_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020060 u32 indent = format_get_indent (s);
Damjan Marion108c7312016-04-20 05:04:20 +020061
62 s = format (s, "netmap: hw_if_index %d next-index %d",
63 t->hw_if_index, t->next_index);
64 s = format (s, "\n%Uslot: flags 0x%x len %u buf_idx %u",
65 format_white_space, indent + 2,
66 t->slot.flags, t->slot.len, t->slot.buf_idx);
67 return s;
68}
69
70always_inline void
Damjan Marion00a9dca2016-08-17 17:05:46 +020071buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
Damjan Marion108c7312016-04-20 05:04:20 +020072{
Damjan Marion00a9dca2016-08-17 17:05:46 +020073 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
74 vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
75 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
Damjan Marion108c7312016-04-20 05:04:20 +020076
77 /* update first buffer */
Damjan Marion00a9dca2016-08-17 17:05:46 +020078 first_b->total_length_not_including_first_buffer += b->current_length;
Damjan Marion108c7312016-04-20 05:04:20 +020079
80 /* update previous buffer */
81 prev_b->next_buffer = bi;
82 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
83
84 /* update current buffer */
85 b->next_buffer = 0;
Damjan Marion108c7312016-04-20 05:04:20 +020086}
87
88always_inline uword
Damjan Marion00a9dca2016-08-17 17:05:46 +020089netmap_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
90 vlib_frame_t * frame, netmap_if_t * nif)
Damjan Marion108c7312016-04-20 05:04:20 +020091{
Damjan Marion8bdc63b2016-11-02 14:48:21 +010092 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Damjan Marion108c7312016-04-20 05:04:20 +020093 uword n_trace = vlib_get_trace_count (vm, node);
Damjan Marion00a9dca2016-08-17 17:05:46 +020094 netmap_main_t *nm = &netmap_main;
Damjan Marion108c7312016-04-20 05:04:20 +020095 u32 n_rx_packets = 0;
96 u32 n_rx_bytes = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +020097 u32 *to_next = 0;
Damjan Marion108c7312016-04-20 05:04:20 +020098 u32 n_free_bufs;
Damjan Marion00a9dca2016-08-17 17:05:46 +020099 struct netmap_ring *ring;
Damjan Marion108c7312016-04-20 05:04:20 +0200100 int cur_ring;
Damjan Marion067cd622018-07-11 12:47:43 +0200101 u32 thread_index = vm->thread_index;
Damjan Marion8934a042019-02-09 23:29:26 +0100102 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Damjan Marion108c7312016-04-20 05:04:20 +0200103
104 if (nif->per_interface_next_index != ~0)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200105 next_index = nif->per_interface_next_index;
Damjan Marion108c7312016-04-20 05:04:20 +0200106
Damjan Marion586afd72017-04-05 19:18:20 +0200107 n_free_bufs = vec_len (nm->rx_buffers[thread_index]);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200108 if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
Damjan Marion108c7312016-04-20 05:04:20 +0200109 {
Damjan Marion586afd72017-04-05 19:18:20 +0200110 vec_validate (nm->rx_buffers[thread_index],
Damjan Marion00a9dca2016-08-17 17:05:46 +0200111 VLIB_FRAME_SIZE + n_free_bufs - 1);
112 n_free_bufs +=
Damjan Marion586afd72017-04-05 19:18:20 +0200113 vlib_buffer_alloc (vm, &nm->rx_buffers[thread_index][n_free_bufs],
Damjan Marion00a9dca2016-08-17 17:05:46 +0200114 VLIB_FRAME_SIZE);
Damjan Marion586afd72017-04-05 19:18:20 +0200115 _vec_len (nm->rx_buffers[thread_index]) = n_free_bufs;
Damjan Marion108c7312016-04-20 05:04:20 +0200116 }
117
118 cur_ring = nif->first_rx_ring;
119 while (cur_ring <= nif->last_rx_ring && n_free_bufs)
120 {
121 int r = 0;
122 u32 cur_slot_index;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200123 ring = NETMAP_RXRING (nif->nifp, cur_ring);
124 r = nm_ring_space (ring);
Damjan Marion108c7312016-04-20 05:04:20 +0200125
126 if (!r)
127 {
128 cur_ring++;
129 continue;
130 }
131
132 if (r > n_free_bufs)
133 r = n_free_bufs;
134
135 cur_slot_index = ring->cur;
136 while (r)
137 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200138 u32 n_left_to_next;
139 u32 next0 = next_index;
140 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Damjan Marion108c7312016-04-20 05:04:20 +0200141
142 while (r && n_left_to_next)
143 {
Damjan Marion109cd502016-11-07 14:05:34 +0100144 vlib_buffer_t *first_b0 = 0;
Damjan Marion108c7312016-04-20 05:04:20 +0200145 u32 offset = 0;
146 u32 bi0 = 0, first_bi0 = 0, prev_bi0;
147 u32 next_slot_index = (cur_slot_index + 1) % ring->num_slots;
148 u32 next2_slot_index = (cur_slot_index + 2) % ring->num_slots;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200149 struct netmap_slot *slot = &ring->slot[cur_slot_index];
Damjan Marion108c7312016-04-20 05:04:20 +0200150 u32 data_len = slot->len;
151
152 /* prefetch 2 slots in advance */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200153 CLIB_PREFETCH (&ring->slot[next2_slot_index],
154 CLIB_CACHE_LINE_BYTES, LOAD);
Damjan Marion108c7312016-04-20 05:04:20 +0200155 /* prefetch start of next packet */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200156 CLIB_PREFETCH (NETMAP_BUF
157 (ring, ring->slot[next_slot_index].buf_idx),
Damjan Marion108c7312016-04-20 05:04:20 +0200158 CLIB_CACHE_LINE_BYTES, LOAD);
159
160 while (data_len && n_free_bufs)
161 {
Damjan Marion109cd502016-11-07 14:05:34 +0100162 vlib_buffer_t *b0;
Damjan Marion108c7312016-04-20 05:04:20 +0200163 /* grab free buffer */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200164 u32 last_empty_buffer =
Damjan Marion586afd72017-04-05 19:18:20 +0200165 vec_len (nm->rx_buffers[thread_index]) - 1;
Damjan Marion108c7312016-04-20 05:04:20 +0200166 prev_bi0 = bi0;
Damjan Marion586afd72017-04-05 19:18:20 +0200167 bi0 = nm->rx_buffers[thread_index][last_empty_buffer];
Damjan Marion108c7312016-04-20 05:04:20 +0200168 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion586afd72017-04-05 19:18:20 +0200169 _vec_len (nm->rx_buffers[thread_index]) = last_empty_buffer;
Damjan Marion108c7312016-04-20 05:04:20 +0200170 n_free_bufs--;
171
172 /* copy data */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200173 u32 bytes_to_copy =
174 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
Damjan Marion108c7312016-04-20 05:04:20 +0200175 b0->current_data = 0;
Dave Barach178cf492018-11-13 16:34:13 -0500176 clib_memcpy_fast (vlib_buffer_get_current (b0),
177 (u8 *) NETMAP_BUF (ring, slot->buf_idx) +
178 offset, bytes_to_copy);
Damjan Marion108c7312016-04-20 05:04:20 +0200179
180 /* fill buffer header */
Damjan Marion108c7312016-04-20 05:04:20 +0200181 b0->current_length = bytes_to_copy;
182
183 if (offset == 0)
184 {
Damjan Marion108c7312016-04-20 05:04:20 +0200185 b0->total_length_not_including_first_buffer = 0;
186 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200187 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
188 nif->sw_if_index;
189 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Damjan Marion108c7312016-04-20 05:04:20 +0200190 first_bi0 = bi0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200191 first_b0 = vlib_get_buffer (vm, first_bi0);
Damjan Marion108c7312016-04-20 05:04:20 +0200192 }
193 else
Damjan Marion00a9dca2016-08-17 17:05:46 +0200194 buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
Damjan Marion108c7312016-04-20 05:04:20 +0200195
196 offset += bytes_to_copy;
197 data_len -= bytes_to_copy;
198 }
199
200 /* trace */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200201 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
202 if (PREDICT_FALSE (n_trace > 0))
203 {
204 if (PREDICT_TRUE (first_b0 != 0))
205 {
206 netmap_input_trace_t *tr;
207 vlib_trace_buffer (vm, node, next0, first_b0,
208 /* follow_chain */ 0);
209 vlib_set_trace_count (vm, node, --n_trace);
210 tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
211 tr->next_index = next0;
212 tr->hw_if_index = nif->hw_if_index;
213 memcpy (&tr->slot, slot, sizeof (struct netmap_slot));
214 }
215 }
Damjan Marion22311502016-10-28 20:30:15 +0200216
217 /* redirect if feature path enabled */
Damjan Marion87cd1192016-11-04 11:00:27 +0100218 vnet_feature_start_device_input_x1 (nif->sw_if_index, &next0,
Damjan Marion35af9e52017-03-06 12:02:50 +0100219 first_b0);
Damjan Marion22311502016-10-28 20:30:15 +0200220
Damjan Marion108c7312016-04-20 05:04:20 +0200221 /* enque and take next packet */
222 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200223 n_left_to_next, first_bi0,
224 next0);
Damjan Marion108c7312016-04-20 05:04:20 +0200225
226 /* next packet */
227 n_rx_packets++;
228 n_rx_bytes += slot->len;
229 to_next[0] = first_bi0;
230 to_next += 1;
231 n_left_to_next--;
232 cur_slot_index = next_slot_index;
233
234 r--;
235 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200236 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Damjan Marion108c7312016-04-20 05:04:20 +0200237 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200238 ring->head = ring->cur = cur_slot_index;
239 cur_ring++;
Damjan Marion108c7312016-04-20 05:04:20 +0200240 }
241
242 if (n_rx_packets)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200243 ioctl (nif->fd, NIOCRXSYNC, NULL);
Damjan Marion108c7312016-04-20 05:04:20 +0200244
245 vlib_increment_combined_counter
Damjan Marion00a9dca2016-08-17 17:05:46 +0200246 (vnet_get_main ()->interface_main.combined_sw_if_counters
Damjan Marion108c7312016-04-20 05:04:20 +0200247 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200248 vlib_get_thread_index (), nif->hw_if_index, n_rx_packets, n_rx_bytes);
Damjan Marion108c7312016-04-20 05:04:20 +0200249
Damjan Marion586afd72017-04-05 19:18:20 +0200250 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
Damjan Marionb3bb1012017-02-28 21:55:28 +0100251
Damjan Marion108c7312016-04-20 05:04:20 +0200252 return n_rx_packets;
253}
254
255static uword
256netmap_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200257 vlib_frame_t * frame)
Damjan Marion108c7312016-04-20 05:04:20 +0200258{
259 int i;
260 u32 n_rx_packets = 0;
Damjan Marion067cd622018-07-11 12:47:43 +0200261 u32 thread_index = vm->thread_index;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200262 netmap_main_t *nm = &netmap_main;
263 netmap_if_t *nmi;
Damjan Marion108c7312016-04-20 05:04:20 +0200264
Damjan Marion00a9dca2016-08-17 17:05:46 +0200265 for (i = 0; i < vec_len (nm->interfaces); i++)
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200266 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200267 nmi = vec_elt_at_index (nm->interfaces, i);
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200268 if (nmi->is_admin_up &&
Damjan Marion00a9dca2016-08-17 17:05:46 +0200269 (i % nm->input_cpu_count) ==
Damjan Marion586afd72017-04-05 19:18:20 +0200270 (thread_index - nm->input_cpu_first_index))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200271 n_rx_packets += netmap_device_input_fn (vm, node, frame, nmi);
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200272 }
Damjan Marion108c7312016-04-20 05:04:20 +0200273
274 return n_rx_packets;
275}
276
Damjan Marion00a9dca2016-08-17 17:05:46 +0200277/* *INDENT-OFF* */
Damjan Marion108c7312016-04-20 05:04:20 +0200278VLIB_REGISTER_NODE (netmap_input_node) = {
279 .function = netmap_input_fn,
280 .name = "netmap-input",
Damjan Marion51327ac2016-11-09 11:59:42 +0100281 .sibling_of = "device-input",
Damjan Marion108c7312016-04-20 05:04:20 +0200282 .format_trace = format_netmap_input_trace,
283 .type = VLIB_NODE_TYPE_INPUT,
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200284 /* default state is INTERRUPT mode, switch to POLLING if worker threads are enabled */
Damjan Marion108c7312016-04-20 05:04:20 +0200285 .state = VLIB_NODE_STATE_INTERRUPT,
286 .n_errors = NETMAP_INPUT_N_ERROR,
287 .error_strings = netmap_input_error_strings,
Damjan Marion108c7312016-04-20 05:04:20 +0200288};
289
Damjan Marion1c80e832016-05-11 23:07:18 +0200290VLIB_NODE_FUNCTION_MULTIARCH (netmap_input_node, netmap_input_fn)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200291/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200292
Damjan Marion00a9dca2016-08-17 17:05:46 +0200293
294/*
295 * fd.io coding-style-patch-verification: ON
296 *
297 * Local Variables:
298 * eval: (c-set-style "gnu")
299 * End:
300 */