blob: 8d700c23e4641961f7fca74c311c5d456e48bfc5 [file] [log] [blame]
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001/*
2 * Copyright (c) 2016 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/dpo/interface_dpo.h>
17#include <vnet/fib/fib_node.h>
18
19/*
20 * The 'DB' of interface DPOs.
21 * There is only one per-interface per-protocol, so this is a per-interface
22 * vector
23 */
24static index_t *interface_dpo_db[DPO_PROTO_NUM];
25
26static interface_dpo_t *
27interface_dpo_alloc (void)
28{
29 interface_dpo_t *ido;
30
31 pool_get(interface_dpo_pool, ido);
32
33 return (ido);
34}
35
36static inline interface_dpo_t *
37interface_dpo_get_from_dpo (const dpo_id_t *dpo)
38{
39 ASSERT(DPO_INTERFACE == dpo->dpoi_type);
40
41 return (interface_dpo_get(dpo->dpoi_index));
42}
43
44static inline index_t
45interface_dpo_get_index (interface_dpo_t *ido)
46{
47 return (ido - interface_dpo_pool);
48}
49
50static void
51interface_dpo_lock (dpo_id_t *dpo)
52{
53 interface_dpo_t *ido;
54
55 ido = interface_dpo_get_from_dpo(dpo);
56 ido->ido_locks++;
57}
58
59static void
60interface_dpo_unlock (dpo_id_t *dpo)
61{
62 interface_dpo_t *ido;
63
64 ido = interface_dpo_get_from_dpo(dpo);
65 ido->ido_locks--;
66
67 if (0 == ido->ido_locks)
68 {
69 interface_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
70 INDEX_INVALID;
71 pool_put(interface_dpo_pool, ido);
72 }
73}
74
75/*
76 * interface_dpo_add_or_lock
77 *
78 * Add/create and lock a new or lock an existing for the interface DPO
79 * on the interface and protocol given
80 */
81void
82interface_dpo_add_or_lock (dpo_proto_t proto,
83 u32 sw_if_index,
84 dpo_id_t *dpo)
85{
86 interface_dpo_t *ido;
87
88 vec_validate_init_empty(interface_dpo_db[proto],
89 sw_if_index,
90 INDEX_INVALID);
91
92 if (INDEX_INVALID == interface_dpo_db[proto][sw_if_index])
93 {
94 ido = interface_dpo_alloc();
95
96 ido->ido_sw_if_index = sw_if_index;
97 ido->ido_proto = proto;
98
99 interface_dpo_db[proto][sw_if_index] =
100 interface_dpo_get_index(ido);
101 }
102 else
103 {
104 ido = interface_dpo_get(interface_dpo_db[proto][sw_if_index]);
105 }
106
107 dpo_set(dpo, DPO_INTERFACE, proto, interface_dpo_get_index(ido));
108}
109
110
111static clib_error_t *
112interface_dpo_interface_state_change (vnet_main_t * vnm,
113 u32 sw_if_index,
114 u32 flags)
115{
116 /*
117 */
118 return (NULL);
119}
120
121VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
122 interface_dpo_interface_state_change);
123
124/**
125 * @brief Registered callback for HW interface state changes
126 */
127static clib_error_t *
128interface_dpo_hw_interface_state_change (vnet_main_t * vnm,
129 u32 hw_if_index,
130 u32 flags)
131{
132 return (NULL);
133}
134
135VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
136 interface_dpo_hw_interface_state_change);
137
138static clib_error_t *
139interface_dpo_interface_delete (vnet_main_t * vnm,
140 u32 sw_if_index,
141 u32 is_add)
142{
143 return (NULL);
144}
145
146VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
147 interface_dpo_interface_delete);
148
149u8*
150format_interface_dpo (u8* s, va_list *ap)
151{
152 index_t index = va_arg(*ap, index_t);
153 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
154 vnet_main_t * vnm = vnet_get_main();
155 interface_dpo_t *ido = interface_dpo_get(index);
156
157 return (format(s, "%U-dpo: %U",
158 format_vnet_sw_interface_name,
159 vnm,
160 vnet_get_sw_interface(vnm, ido->ido_sw_if_index),
161 format_dpo_proto, ido->ido_proto));
162}
163
164static void
165interface_dpo_mem_show (void)
166{
167 fib_show_memory_usage("Interface",
168 pool_elts(interface_dpo_pool),
169 pool_len(interface_dpo_pool),
170 sizeof(interface_dpo_t));
171}
172
173
174const static dpo_vft_t interface_dpo_vft = {
175 .dv_lock = interface_dpo_lock,
176 .dv_unlock = interface_dpo_unlock,
177 .dv_format = format_interface_dpo,
178 .dv_mem_show = interface_dpo_mem_show,
179};
180
181/**
182 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
183 * object.
184 *
185 * this means that these graph nodes are ones from which a glean is the
186 * parent object in the DPO-graph.
187 */
188const static char* const interface_dpo_ip4_nodes[] =
189{
190 "interface-dpo-ip4",
191 NULL,
192};
193const static char* const interface_dpo_ip6_nodes[] =
194{
195 "interface-dpo-ip4",
196 NULL,
197};
198
199const static char* const * const interface_dpo_nodes[DPO_PROTO_NUM] =
200{
201 [DPO_PROTO_IP4] = interface_dpo_ip4_nodes,
202 [DPO_PROTO_IP6] = interface_dpo_ip6_nodes,
203 [DPO_PROTO_MPLS] = NULL,
204};
205
206void
207interface_dpo_module_init (void)
208{
209 dpo_register(DPO_INTERFACE,
210 &interface_dpo_vft,
211 interface_dpo_nodes);
212}
213
214/**
215 * @brief Interface DPO trace data
216 */
217typedef struct interface_dpo_trace_t_
218{
219 u32 sw_if_index;
220} interface_dpo_trace_t;
221
222typedef enum interface_dpo_next_t_
223{
224 INTERFACE_DPO_DROP = 0,
225 INTERFACE_DPO_INPUT = 1,
226} interface_dpo_next_t;
227
228always_inline uword
229interface_dpo_inline (vlib_main_t * vm,
230 vlib_node_runtime_t * node,
231 vlib_frame_t * from_frame)
232{
233 u32 n_left_from, next_index, * from, * to_next;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200234 u32 thread_index = vlib_get_thread_index ();
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800235 vnet_interface_main_t *im;
236
237 im = &vnet_get_main ()->interface_main;
238 from = vlib_frame_vector_args (from_frame);
239 n_left_from = from_frame->n_vectors;
240
241 next_index = node->cached_next_index;
242
243 while (n_left_from > 0)
244 {
245 u32 n_left_to_next;
246
247 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
248
249 while (n_left_from >= 4 && n_left_to_next > 2)
250 {
251 const interface_dpo_t *ido0, *ido1;
252 u32 bi0, idoi0, bi1, idoi1;
253 vlib_buffer_t *b0, *b1;
254
255 bi0 = from[0];
256 to_next[0] = bi0;
257 bi1 = from[1];
258 to_next[1] = bi1;
259 from += 2;
260 to_next += 2;
261 n_left_from -= 2;
262 n_left_to_next -= 2;
263
264 b0 = vlib_get_buffer (vm, bi0);
265 b1 = vlib_get_buffer (vm, bi1);
266
267 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
268 idoi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
269 ido0 = interface_dpo_get(idoi0);
270 ido1 = interface_dpo_get(idoi1);
271
272 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
273 vnet_buffer(b1)->sw_if_index[VLIB_RX] = ido1->ido_sw_if_index;
274
275 vlib_increment_combined_counter (im->combined_sw_if_counters
276 + VNET_INTERFACE_COUNTER_RX,
Damjan Marionf55f9b82017-05-10 21:06:28 +0200277 thread_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800278 ido0->ido_sw_if_index,
279 1,
280 vlib_buffer_length_in_chain (vm, b0));
281 vlib_increment_combined_counter (im->combined_sw_if_counters
282 + VNET_INTERFACE_COUNTER_RX,
Damjan Marionf55f9b82017-05-10 21:06:28 +0200283 thread_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800284 ido1->ido_sw_if_index,
285 1,
286 vlib_buffer_length_in_chain (vm, b1));
287
288 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
289 {
290 interface_dpo_trace_t *tr0;
291
292 tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
293 tr0->sw_if_index = ido0->ido_sw_if_index;
294 }
295 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
296 {
297 interface_dpo_trace_t *tr1;
298
299 tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
300 tr1->sw_if_index = ido1->ido_sw_if_index;
301 }
302
303 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
304 n_left_to_next, bi0, bi1,
305 INTERFACE_DPO_INPUT,
306 INTERFACE_DPO_INPUT);
307 }
308
309 while (n_left_from > 0 && n_left_to_next > 0)
310 {
311 const interface_dpo_t * ido0;
312 vlib_buffer_t * b0;
313 u32 bi0, idoi0;
314
315 bi0 = from[0];
316 to_next[0] = bi0;
317 from += 1;
318 to_next += 1;
319 n_left_from -= 1;
320 n_left_to_next -= 1;
321
322 b0 = vlib_get_buffer (vm, bi0);
323
324 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
325 ido0 = interface_dpo_get(idoi0);
326
327 /* Swap the RX interface of the packet to the one the
328 * interface DPR represents */
329 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
330
331 /* Bump the interface's RX coutners */
332 vlib_increment_combined_counter (im->combined_sw_if_counters
333 + VNET_INTERFACE_COUNTER_RX,
Damjan Marionf55f9b82017-05-10 21:06:28 +0200334 thread_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800335 ido0->ido_sw_if_index,
336 1,
337 vlib_buffer_length_in_chain (vm, b0));
338
339 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
340 {
341 interface_dpo_trace_t *tr;
342
343 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
344 tr->sw_if_index = ido0->ido_sw_if_index;
345 }
346
347 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
348 n_left_to_next, bi0,
349 INTERFACE_DPO_INPUT);
350 }
351 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
352 }
353 return from_frame->n_vectors;
354}
355
356static u8 *
357format_interface_dpo_trace (u8 * s, va_list * args)
358{
359 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
360 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
361 interface_dpo_trace_t * t = va_arg (*args, interface_dpo_trace_t *);
362 uword indent = format_get_indent (s);
363 s = format (s, "%U sw_if_index:%d",
364 format_white_space, indent,
365 t->sw_if_index);
366 return s;
367}
368
369static uword
370interface_dpo_ip4 (vlib_main_t * vm,
371 vlib_node_runtime_t * node,
372 vlib_frame_t * from_frame)
373{
374 return (interface_dpo_inline(vm, node, from_frame));
375}
376
377static uword
378interface_dpo_ip6 (vlib_main_t * vm,
379 vlib_node_runtime_t * node,
380 vlib_frame_t * from_frame)
381{
382 return (interface_dpo_inline(vm, node, from_frame));
383}
384
385VLIB_REGISTER_NODE (interface_dpo_ip4_node) = {
386 .function = interface_dpo_ip4,
387 .name = "interface-dpo-ip4",
388 .vector_size = sizeof (u32),
389 .format_trace = format_interface_dpo_trace,
390
391 .n_next_nodes = 2,
392 .next_nodes = {
393 [INTERFACE_DPO_DROP] = "ip4-drop",
394 [INTERFACE_DPO_INPUT] = "ip4-input",
395 },
396};
397
398VLIB_NODE_FUNCTION_MULTIARCH (interface_dpo_ip4_node,
399 interface_dpo_ip4)
400
401VLIB_REGISTER_NODE (interface_dpo_ip6_node) = {
402 .function = interface_dpo_ip6,
403 .name = "interface-dpo-ip6",
404 .vector_size = sizeof (u32),
405 .format_trace = format_interface_dpo_trace,
406
407 .n_next_nodes = 2,
408 .next_nodes = {
409 [INTERFACE_DPO_DROP] = "ip6-drop",
410 [INTERFACE_DPO_INPUT] = "ip6-input",
411 },
412};
413
414VLIB_NODE_FUNCTION_MULTIARCH (interface_dpo_ip6_node,
415 interface_dpo_ip6)
416