2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 vlib_node_registration_t ssvm_eth_input_node;
23 } ssvm_eth_input_trace_t;
25 /* packet trace format function */
27 format_ssvm_eth_input_trace (u8 * s, va_list * args)
29 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
30 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
31 ssvm_eth_input_trace_t *t = va_arg (*args, ssvm_eth_input_trace_t *);
33 s = format (s, "SSVM_ETH_INPUT: sw_if_index %d, next index %d",
34 t->sw_if_index, t->next_index);
38 vlib_node_registration_t ssvm_eth_input_node;
40 #define foreach_ssvm_eth_input_error \
41 _(NO_BUFFERS, "Rx packet drops (no buffers)")
45 #define _(sym,str) SSVM_ETH_INPUT_ERROR_##sym,
46 foreach_ssvm_eth_input_error
48 SSVM_ETH_INPUT_N_ERROR,
49 } ssvm_eth_input_error_t;
51 static char *ssvm_eth_input_error_strings[] = {
52 #define _(sym,string) string,
53 foreach_ssvm_eth_input_error
59 SSVM_ETH_INPUT_NEXT_DROP,
60 SSVM_ETH_INPUT_NEXT_ETHERNET_INPUT,
61 SSVM_ETH_INPUT_NEXT_IP4_INPUT,
62 SSVM_ETH_INPUT_NEXT_IP6_INPUT,
63 SSVM_ETH_INPUT_NEXT_MPLS_INPUT,
64 SSVM_ETH_INPUT_N_NEXT,
65 } ssvm_eth_input_next_t;
68 ssvm_eth_device_input (ssvm_eth_main_t * em,
69 ssvm_private_t * intfc, vlib_node_runtime_t * node)
71 ssvm_shared_header_t *sh = intfc->sh;
72 vlib_main_t *vm = em->vlib_main;
73 unix_shared_memory_queue_t *q;
74 ssvm_eth_queue_elt_t *elt, *elts;
76 u32 my_pid = intfc->my_pid;
78 u32 n_to_alloc = VLIB_FRAME_SIZE * 2;
79 u32 n_allocated, n_present_in_cache;
81 u32 next_index = DPDK_RX_NEXT_ETHERNET_INPUT;
85 vlib_buffer_free_list_t *fl;
86 u32 n_left_to_next, *to_next;
91 vlib_buffer_t *b0, *prev;
92 u32 saved_cache_size = 0;
93 ethernet_header_t *eh0;
95 u32 n_rx_bytes = 0, l3_offset0;
96 u32 cpu_index = os_get_cpu_number ();
97 u32 trace_cnt __attribute__ ((unused)) = vlib_get_trace_count (vm, node);
100 uword n_trace = vlib_get_trace_count (vm, node);
102 /* Either side down? buh-bye... */
103 if (pointer_to_uword (sh->opaque[MASTER_ADMIN_STATE_INDEX]) == 0 ||
104 pointer_to_uword (sh->opaque[SLAVE_ADMIN_STATE_INDEX]) == 0)
107 if (intfc->i_am_master)
108 q = (unix_shared_memory_queue_t *) (sh->opaque[TO_MASTER_Q_INDEX]);
110 q = (unix_shared_memory_queue_t *) (sh->opaque[TO_SLAVE_Q_INDEX]);
116 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
118 vec_reset_length (intfc->rx_queue);
121 while (__sync_lock_test_and_set (lock, 1))
123 while (q->cursize > 0)
125 unix_shared_memory_queue_sub_raw (q, (u8 *) & elt_index);
126 ASSERT (elt_index < 2048);
127 vec_add1 (intfc->rx_queue, elt_index);
129 CLIB_MEMORY_BARRIER ();
132 n_present_in_cache = vec_len (em->buffer_cache);
134 if (vec_len (em->buffer_cache) < vec_len (intfc->rx_queue) * 2)
136 vec_validate (em->buffer_cache,
137 n_to_alloc + vec_len (em->buffer_cache) - 1);
139 vlib_buffer_alloc (vm, &em->buffer_cache[n_present_in_cache],
142 n_present_in_cache += n_allocated;
143 _vec_len (em->buffer_cache) = n_present_in_cache;
146 elts = (ssvm_eth_queue_elt_t *) (sh->opaque[CHUNK_POOL_INDEX]);
148 n_buffers = vec_len (intfc->rx_queue);
151 while (n_buffers > 0)
153 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
155 while (n_buffers > 0 && n_left_to_next > 0)
157 elt = elts + intfc->rx_queue[rx_queue_index];
159 saved_cache_size = n_present_in_cache;
160 if (PREDICT_FALSE (saved_cache_size == 0))
162 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
165 saved_bi0 = bi0 = em->buffer_cache[--n_present_in_cache];
166 b0 = vlib_get_buffer (vm, bi0);
171 vlib_buffer_init_for_free_list (b0, fl);
173 b0->current_data = elt->current_data_hint;
174 b0->current_length = elt->length_this_buffer;
175 b0->total_length_not_including_first_buffer =
176 elt->total_length_not_including_first_buffer;
178 clib_memcpy (b0->data + b0->current_data, elt->data,
181 if (PREDICT_FALSE (prev != 0))
182 prev->next_buffer = bi0;
184 if (PREDICT_FALSE (elt->flags & SSVM_BUFFER_NEXT_PRESENT))
187 if (PREDICT_FALSE (n_present_in_cache == 0))
189 vlib_put_next_frame (vm, node, next_index,
193 bi0 = em->buffer_cache[--n_present_in_cache];
194 b0 = vlib_get_buffer (vm, bi0);
200 saved_cache_size = n_present_in_cache;
202 to_next[0] = saved_bi0;
206 b0 = vlib_get_buffer (vm, saved_bi0);
207 eh0 = vlib_buffer_get_current (b0);
209 type0 = clib_net_to_host_u16 (eh0->type);
211 next0 = SSVM_ETH_INPUT_NEXT_ETHERNET_INPUT;
213 if (type0 == ETHERNET_TYPE_IP4)
214 next0 = SSVM_ETH_INPUT_NEXT_IP4_INPUT;
215 else if (type0 == ETHERNET_TYPE_IP6)
216 next0 = SSVM_ETH_INPUT_NEXT_IP6_INPUT;
217 else if (type0 == ETHERNET_TYPE_MPLS_UNICAST)
218 next0 = SSVM_ETH_INPUT_NEXT_MPLS_INPUT;
220 l3_offset0 = ((next0 == SSVM_ETH_INPUT_NEXT_IP4_INPUT ||
221 next0 == SSVM_ETH_INPUT_NEXT_IP6_INPUT ||
222 next0 == SSVM_ETH_INPUT_NEXT_MPLS_INPUT) ?
223 sizeof (ethernet_header_t) : 0);
225 n_rx_bytes += b0->current_length
226 + b0->total_length_not_including_first_buffer;
228 b0->current_data += l3_offset0;
229 b0->current_length -= l3_offset0;
230 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
232 vnet_buffer (b0)->sw_if_index[VLIB_RX] = intfc->vlib_hw_if_index;
233 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
236 * Turn this on if you run into
237 * "bad monkey" contexts, and you want to know exactly
238 * which nodes they've visited... See main.c...
240 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
242 if (PREDICT_FALSE (n_trace > 0))
244 ssvm_eth_input_trace_t *tr;
246 vlib_trace_buffer (vm, node, next0, b0, /* follow_chain */ 1);
247 vlib_set_trace_count (vm, node, --n_trace);
249 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
251 tr->next_index = next0;
252 tr->sw_if_index = intfc->vlib_hw_if_index;
255 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
256 to_next, n_left_to_next,
262 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
266 if (em->buffer_cache)
267 _vec_len (em->buffer_cache) = saved_cache_size;
269 ASSERT (saved_cache_size == 0);
271 ssvm_lock (sh, my_pid, 2);
273 ASSERT (vec_len (intfc->rx_queue) > 0);
275 n_available = (u32) pointer_to_uword (sh->opaque[CHUNK_POOL_NFREE]);
276 elt_indices = (u32 *) (sh->opaque[CHUNK_POOL_FREELIST_INDEX]);
278 clib_memcpy (&elt_indices[n_available], intfc->rx_queue,
279 vec_len (intfc->rx_queue) * sizeof (u32));
281 n_available += vec_len (intfc->rx_queue);
282 sh->opaque[CHUNK_POOL_NFREE] = uword_to_pointer (n_available, void *);
286 vlib_error_count (vm, node->node_index, SSVM_ETH_INPUT_ERROR_NO_BUFFERS,
289 vlib_increment_combined_counter
290 (vnet_get_main ()->interface_main.combined_sw_if_counters
291 + VNET_INTERFACE_COUNTER_RX, cpu_index,
292 intfc->vlib_hw_if_index, rx_queue_index, n_rx_bytes);
294 return rx_queue_index;
298 ssvm_eth_input_node_fn (vlib_main_t * vm,
299 vlib_node_runtime_t * node, vlib_frame_t * frame)
301 ssvm_eth_main_t *em = &ssvm_eth_main;
302 ssvm_private_t *intfc;
303 uword n_rx_packets = 0;
305 vec_foreach (intfc, em->intfcs)
307 n_rx_packets += ssvm_eth_device_input (em, intfc, node);
314 VLIB_REGISTER_NODE (ssvm_eth_input_node) = {
315 .function = ssvm_eth_input_node_fn,
316 .name = "ssvm_eth_input",
317 .vector_size = sizeof (u32),
318 .format_trace = format_ssvm_eth_input_trace,
319 .type = VLIB_NODE_TYPE_INPUT,
320 .state = VLIB_NODE_STATE_DISABLED,
322 .n_errors = ARRAY_LEN(ssvm_eth_input_error_strings),
323 .error_strings = ssvm_eth_input_error_strings,
325 .n_next_nodes = SSVM_ETH_INPUT_N_NEXT,
327 /* edit / add dispositions here */
329 [SSVM_ETH_INPUT_NEXT_DROP] = "error-drop",
330 [SSVM_ETH_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",
331 [SSVM_ETH_INPUT_NEXT_IP4_INPUT] = "ip4-input",
332 [SSVM_ETH_INPUT_NEXT_IP6_INPUT] = "ip6-input",
333 [SSVM_ETH_INPUT_NEXT_MPLS_INPUT] = "mpls-input",
337 VLIB_NODE_FUNCTION_MULTIARCH (ssvm_eth_input_node, ssvm_eth_input_node_fn)
342 * fd.io coding-style-patch-verification: ON
345 * eval: (c-set-style "gnu")