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.
16 #ifndef __IP_PUNT_DROP_H__
17 #define __IP_PUNT_DROP_H__
19 #include <vnet/ip/ip.h>
20 #include <vnet/policer/policer.h>
21 #include <vnet/policer/police_inlines.h>
24 * IP4 punt policer configuration
25 * we police the punt rate to prevent overloading the host
27 typedef struct ip_punt_policer_t_
33 typedef enum ip_punt_policer_next_t_
35 IP_PUNT_POLICER_NEXT_DROP,
36 IP_PUNT_POLICER_NEXT_HANDOFF,
37 IP_PUNT_POLICER_N_NEXT,
38 } ip_punt_policer_next_t;
40 typedef struct ip_punt_policer_trace_t_
44 } ip_punt_policer_trace_t;
46 #define foreach_ip_punt_policer_error \
47 _(DROP, "ip punt policer drop")
51 #define _(sym,str) IP_PUNT_POLICER_ERROR_##sym,
52 foreach_ip_punt_policer_error
54 IP4_PUNT_POLICER_N_ERROR,
55 } ip_punt_policer_error_t;
57 extern u8 *format_ip_punt_policer_trace (u8 * s, va_list * args);
58 extern vlib_node_registration_t ip4_punt_policer_node;
59 extern ip_punt_policer_t ip4_punt_policer_cfg;
60 extern vlib_node_registration_t ip6_punt_policer_node;
61 extern ip_punt_policer_t ip6_punt_policer_cfg;
64 * IP punt policing node function
67 ip_punt_policer (vlib_main_t * vm,
68 vlib_node_runtime_t * node,
69 vlib_frame_t * frame, u8 arc_index, u32 policer_index)
71 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
72 u64 time_in_policer_periods;
73 vnet_feature_main_t *fm = &feature_main;
74 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
75 vnet_policer_main_t *pm = &vnet_policer_main;
76 policer_read_response_type_st *pol = &pm->policers[policer_index];
77 u32 pol_thread_index = pol->thread_index;
78 u32 this_thread_index = vm->thread_index;
80 time_in_policer_periods =
81 clib_cpu_time_now () >> POLICER_TICKS_PER_PERIOD_SHIFT;
83 from = vlib_frame_vector_args (frame);
84 n_left_from = frame->n_vectors;
85 next_index = node->cached_next_index;
87 if (PREDICT_FALSE (pol_thread_index == ~0))
90 * This is the first packet to use this policer. Set the
91 * thread index in the policer to this thread and any
92 * packets seen by this node on other threads will
93 * be handed off to this one.
95 * This could happen simultaneously on another thread.
97 clib_atomic_cmp_and_swap (&pol->thread_index, ~0, this_thread_index);
98 pol_thread_index = this_thread_index;
101 while (n_left_from > 0)
103 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
105 while (n_left_from >= 4 && n_left_to_next >= 2)
107 vlib_buffer_t *b0, *b1;
113 bi0 = to_next[0] = from[0];
114 bi1 = to_next[1] = from[1];
121 b0 = vlib_get_buffer (vm, bi0);
122 b1 = vlib_get_buffer (vm, bi1);
124 if (PREDICT_FALSE (this_thread_index != pol_thread_index))
126 next0 = next1 = IP_PUNT_POLICER_NEXT_HANDOFF;
131 vnet_get_config_data (&cm->config_main,
132 &b0->current_config_index, &next0, 0);
133 vnet_get_config_data (&cm->config_main,
134 &b1->current_config_index, &next1, 0);
137 vnet_policer_police (vm, b0, policer_index,
138 time_in_policer_periods, POLICE_CONFORM);
140 vnet_policer_police (vm, b1, policer_index,
141 time_in_policer_periods, POLICE_CONFORM);
143 if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
145 next0 = IP_PUNT_POLICER_NEXT_DROP;
146 b0->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
148 if (PREDICT_FALSE (act1 == SSE2_QOS_ACTION_DROP))
150 next1 = IP_PUNT_POLICER_NEXT_DROP;
151 b1->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
154 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
156 ip_punt_policer_trace_t *t =
157 vlib_add_trace (vm, node, b0, sizeof (*t));
159 t->policer_index = policer_index;
161 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
163 ip_punt_policer_trace_t *t =
164 vlib_add_trace (vm, node, b1, sizeof (*t));
166 t->policer_index = policer_index;
170 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
172 bi0, bi1, next0, next1);
174 while (n_left_from > 0 && n_left_to_next > 0)
182 bi0 = to_next[0] = from[0];
189 b0 = vlib_get_buffer (vm, bi0);
191 if (PREDICT_FALSE (this_thread_index != pol_thread_index))
193 next0 = IP_PUNT_POLICER_NEXT_HANDOFF;
197 vnet_get_config_data (&cm->config_main,
198 &b0->current_config_index, &next0, 0);
201 vnet_policer_police (vm, b0, policer_index,
202 time_in_policer_periods, POLICE_CONFORM);
203 if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
205 next0 = IP_PUNT_POLICER_NEXT_DROP;
206 b0->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
209 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
211 ip_punt_policer_trace_t *t =
212 vlib_add_trace (vm, node, b0, sizeof (*t));
214 t->policer_index = policer_index;
217 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
218 n_left_to_next, bi0, next0);
220 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
223 return frame->n_vectors;
227 * IP4 punt redirect per-rx interface configuration
228 * redirect punted traffic to another location.
230 typedef struct ip_punt_redirect_rx_t_
233 * Node linkage into the FIB graph
237 fib_protocol_t fproto;
238 fib_forward_chain_type_t payload_type;
243 * redirect forwarding
246 } ip_punt_redirect_rx_t;
249 * IP punt redirect configuration
251 typedef struct ip_punt_redirect_t_
253 ip_punt_redirect_rx_t *pool;
256 * per-RX interface configuration.
257 * sw_if_index = 0 (from which packets are never received) is used to
258 * indicate 'from-any'
260 index_t *redirect_by_rx_sw_if_index[FIB_PROTOCOL_IP_MAX];
261 } ip_punt_redirect_cfg_t;
263 extern ip_punt_redirect_cfg_t ip_punt_redirect_cfg;
266 * IP punt redirect next nodes
268 typedef enum ip_punt_redirect_next_t_
270 IP_PUNT_REDIRECT_NEXT_DROP,
271 IP_PUNT_REDIRECT_NEXT_TX,
272 IP_PUNT_REDIRECT_NEXT_ARP,
273 IP_PUNT_REDIRECT_N_NEXT,
274 } ip_punt_redirect_next_t;
277 * IP Punt redirect trace
279 typedef struct ip4_punt_redirect_trace_t_
283 } ip_punt_redirect_trace_t;
286 * Add a punt redirect entry
288 extern void ip_punt_redirect_add (fib_protocol_t fproto,
290 fib_forward_chain_type_t ct,
291 fib_route_path_t * rpaths);
293 extern void ip_punt_redirect_del (fib_protocol_t fproto, u32 rx_sw_if_index);
294 extern index_t ip_punt_redirect_find (fib_protocol_t fproto,
296 extern u8 *format_ip_punt_redirect (u8 * s, va_list * args);
298 extern u8 *format_ip_punt_redirect_trace (u8 * s, va_list * args);
300 typedef walk_rc_t (*ip_punt_redirect_walk_cb_t) (u32 rx_sw_if_index,
301 const ip_punt_redirect_rx_t *
302 redirect, void *arg);
303 extern void ip_punt_redirect_walk (fib_protocol_t fproto,
304 ip_punt_redirect_walk_cb_t cb, void *ctx);
306 static_always_inline ip_punt_redirect_rx_t *
307 ip_punt_redirect_get (index_t rrxi)
309 return (pool_elt_at_index (ip_punt_redirect_cfg.pool, rrxi));
313 ip_punt_redirect (vlib_main_t * vm,
314 vlib_node_runtime_t * node,
315 vlib_frame_t * frame, u8 arc_index, fib_protocol_t fproto)
317 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
318 vnet_feature_main_t *fm = &feature_main;
319 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
322 from = vlib_frame_vector_args (frame);
323 n_left_from = frame->n_vectors;
324 next_index = node->cached_next_index;
325 redirects = ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto];
327 while (n_left_from > 0)
329 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
331 while (n_left_from > 0 && n_left_to_next > 0)
333 u32 rx_sw_if_index0, rrxi0;
334 ip_punt_redirect_rx_t *rrx0;
339 rrxi0 = INDEX_INVALID;
341 bi0 = to_next[0] = from[0];
348 b0 = vlib_get_buffer (vm, bi0);
350 vnet_get_config_data (&cm->config_main,
351 &b0->current_config_index, &next0, 0);
353 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
356 * If config exists for this particular RX interface use it,
357 * else use the default (at RX = 0)
359 if (vec_len (redirects) > rx_sw_if_index0)
361 rrxi0 = redirects[rx_sw_if_index0];
362 if (INDEX_INVALID == rrxi0)
363 rrxi0 = redirects[0];
365 else if (vec_len (redirects) >= 1)
366 rrxi0 = redirects[0];
368 if (PREDICT_TRUE (INDEX_INVALID != rrxi0))
370 rrx0 = ip_punt_redirect_get (rrxi0);
371 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = rrx0->dpo.dpoi_index;
372 next0 = rrx0->dpo.dpoi_next_node;
375 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
377 ip_punt_redirect_trace_t *t =
378 vlib_add_trace (vm, node, b0, sizeof (*t));
383 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
384 n_left_to_next, bi0, next0);
387 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
390 return frame->n_vectors;
394 ip_drop_or_punt (vlib_main_t * vm,
395 vlib_node_runtime_t * node,
396 vlib_frame_t * frame, u8 arc_index)
398 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
400 from = vlib_frame_vector_args (frame);
401 n_left_from = frame->n_vectors;
402 next_index = node->cached_next_index;
404 while (n_left_from > 0)
406 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
408 while (n_left_from >= 8 && n_left_to_next >= 4)
410 vlib_buffer_t *b0, *b1, *b2, *b3;
411 u32 next0, next1, next2, next3;
412 u32 bi0, bi1, bi2, bi3;
414 next0 = next1 = next2 = next3 = 0;
416 /* Prefetch next iteration. */
418 vlib_buffer_t *p4, *p5, *p6, *p7;
420 p4 = vlib_get_buffer (vm, from[4]);
421 p5 = vlib_get_buffer (vm, from[5]);
422 p6 = vlib_get_buffer (vm, from[6]);
423 p7 = vlib_get_buffer (vm, from[7]);
425 vlib_prefetch_buffer_header (p4, LOAD);
426 vlib_prefetch_buffer_header (p5, LOAD);
427 vlib_prefetch_buffer_header (p6, LOAD);
428 vlib_prefetch_buffer_header (p7, LOAD);
431 bi0 = to_next[0] = from[0];
432 bi1 = to_next[1] = from[1];
433 bi2 = to_next[2] = from[2];
434 bi3 = to_next[3] = from[3];
441 b0 = vlib_get_buffer (vm, bi0);
442 b1 = vlib_get_buffer (vm, bi1);
443 b2 = vlib_get_buffer (vm, bi2);
444 b3 = vlib_get_buffer (vm, bi3);
446 /* punt and drop features are not associated with a given interface
447 * so the special index 0 is used */
448 vnet_feature_arc_start (arc_index, 0, &next0, b0);
449 vnet_feature_arc_start (arc_index, 0, &next1, b1);
450 vnet_feature_arc_start (arc_index, 0, &next2, b2);
451 vnet_feature_arc_start (arc_index, 0, &next3, b3);
453 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
454 to_next, n_left_to_next,
456 next0, next1, next2, next3);
459 while (n_left_from > 0 && n_left_to_next > 0)
466 bi0 = to_next[0] = from[0];
473 b0 = vlib_get_buffer (vm, bi0);
475 vnet_feature_arc_start (arc_index, 0, &next0, b0);
477 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
478 n_left_to_next, bi0, next0);
480 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
483 return frame->n_vectors;
489 * fd.io coding-style-patch-verification: ON
492 * eval: (c-set-style "gnu")