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_
32 typedef enum ip_punt_policer_next_t_
34 IP_PUNT_POLICER_NEXT_DROP,
35 IP_PUNT_POLICER_N_NEXT,
36 } ip_punt_policer_next_t;
38 typedef struct ip_punt_policer_trace_t_
42 } ip_punt_policer_trace_t;
44 #define foreach_ip_punt_policer_error \
45 _(DROP, "ip punt policer drop")
49 #define _(sym,str) IP_PUNT_POLICER_ERROR_##sym,
50 foreach_ip_punt_policer_error
52 IP4_PUNT_POLICER_N_ERROR,
53 } ip_punt_policer_error_t;
55 extern u8 *format_ip_punt_policer_trace (u8 * s, va_list * args);
58 * IP punt policing node function
61 ip_punt_policer (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
63 vlib_frame_t * frame, u8 arc_index, u32 policer_index)
65 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
66 u64 time_in_policer_periods;
67 vnet_feature_main_t *fm = &feature_main;
68 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
70 time_in_policer_periods =
71 clib_cpu_time_now () >> POLICER_TICKS_PER_PERIOD_SHIFT;
73 from = vlib_frame_vector_args (frame);
74 n_left_from = frame->n_vectors;
75 next_index = node->cached_next_index;
77 while (n_left_from > 0)
79 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
81 while (n_left_from >= 4 && n_left_to_next >= 2)
83 vlib_buffer_t *b0, *b1;
89 bi0 = to_next[0] = from[0];
90 bi1 = to_next[1] = from[1];
97 b0 = vlib_get_buffer (vm, bi0);
98 b1 = vlib_get_buffer (vm, bi1);
100 vnet_get_config_data (&cm->config_main,
101 &b0->current_config_index, &next0, 0);
102 vnet_get_config_data (&cm->config_main,
103 &b1->current_config_index, &next1, 0);
105 act0 = vnet_policer_police (vm, b0,
107 time_in_policer_periods,
109 act1 = vnet_policer_police (vm, b1,
111 time_in_policer_periods,
114 if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
116 next0 = IP_PUNT_POLICER_NEXT_DROP;
117 b0->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
119 if (PREDICT_FALSE (act1 == SSE2_QOS_ACTION_DROP))
121 next1 = IP_PUNT_POLICER_NEXT_DROP;
122 b1->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
125 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
127 ip_punt_policer_trace_t *t =
128 vlib_add_trace (vm, node, b0, sizeof (*t));
130 t->policer_index = policer_index;
132 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
134 ip_punt_policer_trace_t *t =
135 vlib_add_trace (vm, node, b1, sizeof (*t));
137 t->policer_index = policer_index;
139 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
141 bi0, bi1, next0, next1);
143 while (n_left_from > 0 && n_left_to_next > 0)
151 bi0 = to_next[0] = from[0];
158 b0 = vlib_get_buffer (vm, bi0);
160 vnet_get_config_data (&cm->config_main,
161 &b0->current_config_index, &next0, 0);
163 act0 = vnet_policer_police (vm, b0,
165 time_in_policer_periods,
167 if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
169 next0 = IP_PUNT_POLICER_NEXT_DROP;
170 b0->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
173 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
175 ip_punt_policer_trace_t *t =
176 vlib_add_trace (vm, node, b0, sizeof (*t));
178 t->policer_index = policer_index;
181 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
182 n_left_to_next, bi0, next0);
184 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
187 return frame->n_vectors;
191 * IP4 punt redirect per-rx interface configuration
192 * redirect punted traffic to another location.
194 typedef struct ip_punt_redirect_rx_t_
197 * The next-hop to send redirected packets to
202 * the TX interface to send redirected packets
207 * redirect forwarding adjacency
209 adj_index_t adj_index;
210 } ip_punt_redirect_rx_t;
213 * IP punt redirect configuration
215 typedef struct ip_punt_redirect_t_
218 * any RX interface redirect
220 ip_punt_redirect_rx_t any_rx_sw_if_index;
223 * per-RX interface configuration
225 ip_punt_redirect_rx_t *redirect_by_rx_sw_if_index;
226 } ip_punt_redirect_t;
229 * IP punt redirect next nodes
231 typedef enum ip_punt_redirect_next_t_
233 IP_PUNT_REDIRECT_NEXT_DROP,
234 IP_PUNT_REDIRECT_NEXT_TX,
235 IP_PUNT_REDIRECT_NEXT_ARP,
236 IP_PUNT_REDIRECT_N_NEXT,
237 } ip_punt_redirect_next_t;
240 * IP Punt redirect trace
242 typedef struct ip4_punt_redirect_trace_t_
244 ip_punt_redirect_rx_t redirect;
246 } ip_punt_redirect_trace_t;
248 typedef struct ip_punt_redirect_detail_t_
255 * IP punt redirect configuration
257 ip_punt_redirect_rx_t punt_redirect;
258 } ip_punt_redirect_detail_t;
261 * Add a punt redirect entry
263 extern void ip_punt_redirect_add (ip_punt_redirect_t * cfg,
265 ip_punt_redirect_rx_t * redirect,
266 fib_protocol_t fproto, vnet_link_t linkt);
267 extern void ip_punt_redirect_del (ip_punt_redirect_t * cfg,
269 extern u8 *format_ip_punt_redirect (u8 * s, va_list * args);
271 extern u8 *format_ip_punt_redirect_trace (u8 * s, va_list * args);
273 extern ip_punt_redirect_detail_t *ip4_punt_redirect_entries (u32 sw_if_index);
274 extern ip_punt_redirect_detail_t *ip6_punt_redirect_entries (u32 sw_if_index);
277 ip_punt_redirect_tx_via_adj (vlib_buffer_t * b0, adj_index_t ai)
279 ip_adjacency_t *adj = adj_get (ai);
282 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ai;
284 switch (adj->lookup_next_index)
286 case IP_LOOKUP_NEXT_ARP:
287 next0 = IP_PUNT_REDIRECT_NEXT_ARP;
289 case IP_LOOKUP_NEXT_REWRITE:
290 next0 = IP_PUNT_REDIRECT_NEXT_TX;
293 next0 = IP_PUNT_REDIRECT_NEXT_DROP;
301 ip_punt_redirect (vlib_main_t * vm,
302 vlib_node_runtime_t * node,
303 vlib_frame_t * frame,
304 u8 arc_index, ip_punt_redirect_t * redirect)
306 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
307 vnet_feature_main_t *fm = &feature_main;
308 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
310 from = vlib_frame_vector_args (frame);
311 n_left_from = frame->n_vectors;
312 next_index = node->cached_next_index;
314 while (n_left_from > 0)
316 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
318 while (n_left_from > 0 && n_left_to_next > 0)
321 ip_punt_redirect_rx_t *rrx0;
328 bi0 = to_next[0] = from[0];
335 b0 = vlib_get_buffer (vm, bi0);
337 vnet_get_config_data (&cm->config_main,
338 &b0->current_config_index, &next0, 0);
340 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
342 if (vec_len (redirect->redirect_by_rx_sw_if_index) >
345 rrx0 = &redirect->redirect_by_rx_sw_if_index[rx_sw_if_index0];
346 if (~0 != rrx0->tx_sw_if_index)
348 next0 = ip_punt_redirect_tx_via_adj (b0, rrx0->adj_index);
350 else if (~0 != redirect->any_rx_sw_if_index.tx_sw_if_index)
352 rrx0 = &redirect->any_rx_sw_if_index;
353 next0 = ip_punt_redirect_tx_via_adj (b0, rrx0->adj_index);
356 else if (~0 != redirect->any_rx_sw_if_index.tx_sw_if_index)
358 rrx0 = &redirect->any_rx_sw_if_index;
359 next0 = ip_punt_redirect_tx_via_adj (b0, rrx0->adj_index);
362 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
364 ip_punt_redirect_trace_t *t =
365 vlib_add_trace (vm, node, b0, sizeof (*t));
371 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
372 n_left_to_next, bi0, next0);
375 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
378 return frame->n_vectors;
382 ip_drop_or_punt (vlib_main_t * vm,
383 vlib_node_runtime_t * node,
384 vlib_frame_t * frame, u8 arc_index)
386 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
388 from = vlib_frame_vector_args (frame);
389 n_left_from = frame->n_vectors;
390 next_index = node->cached_next_index;
392 while (n_left_from > 0)
394 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
396 while (n_left_from >= 8 && n_left_to_next >= 4)
398 vlib_buffer_t *b0, *b1, *b2, *b3;
399 u32 next0, next1, next2, next3;
400 u32 bi0, bi1, bi2, bi3;
402 next0 = next1 = next2 = next3 = 0;
404 /* Prefetch next iteration. */
406 vlib_buffer_t *p4, *p5, *p6, *p7;
408 p4 = vlib_get_buffer (vm, from[4]);
409 p5 = vlib_get_buffer (vm, from[5]);
410 p6 = vlib_get_buffer (vm, from[6]);
411 p7 = vlib_get_buffer (vm, from[7]);
413 vlib_prefetch_buffer_header (p4, LOAD);
414 vlib_prefetch_buffer_header (p5, LOAD);
415 vlib_prefetch_buffer_header (p6, LOAD);
416 vlib_prefetch_buffer_header (p7, LOAD);
419 bi0 = to_next[0] = from[0];
420 bi1 = to_next[1] = from[1];
421 bi2 = to_next[2] = from[2];
422 bi3 = to_next[3] = from[3];
429 b0 = vlib_get_buffer (vm, bi0);
430 b1 = vlib_get_buffer (vm, bi1);
431 b2 = vlib_get_buffer (vm, bi2);
432 b3 = vlib_get_buffer (vm, bi3);
434 /* punt and drop features are not associated with a given interface
435 * so the special index 0 is used */
436 vnet_feature_arc_start (arc_index, 0, &next0, b0);
437 vnet_feature_arc_start (arc_index, 0, &next1, b1);
438 vnet_feature_arc_start (arc_index, 0, &next2, b2);
439 vnet_feature_arc_start (arc_index, 0, &next3, b3);
441 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
442 to_next, n_left_to_next,
444 next0, next1, next2, next3);
447 while (n_left_from > 0 && n_left_to_next > 0)
454 bi0 = to_next[0] = from[0];
461 b0 = vlib_get_buffer (vm, bi0);
463 vnet_feature_arc_start (arc_index, 0, &next0, b0);
465 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
466 n_left_to_next, bi0, next0);
468 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
471 return frame->n_vectors;
477 * fd.io coding-style-patch-verification: ON
480 * eval: (c-set-style "gnu")