blob: 11affcb6e28aca3d528eabeaa917488c130e9516 [file] [log] [blame]
Neale Rannsd91c1db2017-07-31 02:30:50 -07001/*
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:
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#ifndef __IP_PUNT_DROP_H__
17#define __IP_PUNT_DROP_H__
18
19#include <vnet/ip/ip.h>
20#include <vnet/policer/policer.h>
21#include <vnet/policer/police_inlines.h>
22
23/**
24 * IP4 punt policer configuration
25 * we police the punt rate to prevent overloading the host
26 */
27typedef struct ip_punt_policer_t_
28{
29 u32 policer_index;
Brian Russellbaebb222021-01-19 16:48:56 +000030 u32 fq_index;
Neale Rannsd91c1db2017-07-31 02:30:50 -070031} ip_punt_policer_t;
32
33typedef enum ip_punt_policer_next_t_
34{
35 IP_PUNT_POLICER_NEXT_DROP,
Brian Russell1b2e16a2021-01-21 14:44:09 +000036 IP_PUNT_POLICER_NEXT_HANDOFF,
Neale Rannsd91c1db2017-07-31 02:30:50 -070037 IP_PUNT_POLICER_N_NEXT,
38} ip_punt_policer_next_t;
39
40typedef struct ip_punt_policer_trace_t_
41{
42 u32 policer_index;
43 u32 next;
44} ip_punt_policer_trace_t;
45
46#define foreach_ip_punt_policer_error \
47_(DROP, "ip punt policer drop")
48
49typedef enum
50{
51#define _(sym,str) IP_PUNT_POLICER_ERROR_##sym,
52 foreach_ip_punt_policer_error
53#undef _
54 IP4_PUNT_POLICER_N_ERROR,
55} ip_punt_policer_error_t;
56
57extern u8 *format_ip_punt_policer_trace (u8 * s, va_list * args);
Brian Russellbaebb222021-01-19 16:48:56 +000058extern vlib_node_registration_t ip4_punt_policer_node;
59extern ip_punt_policer_t ip4_punt_policer_cfg;
60extern vlib_node_registration_t ip6_punt_policer_node;
61extern ip_punt_policer_t ip6_punt_policer_cfg;
Neale Rannsd91c1db2017-07-31 02:30:50 -070062
63/**
64 * IP punt policing node function
65 */
66always_inline uword
67ip_punt_policer (vlib_main_t * vm,
68 vlib_node_runtime_t * node,
69 vlib_frame_t * frame, u8 arc_index, u32 policer_index)
70{
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];
Brian Russell1b2e16a2021-01-21 14:44:09 +000075 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;
Neale Rannsd91c1db2017-07-31 02:30:50 -070079
80 time_in_policer_periods =
81 clib_cpu_time_now () >> POLICER_TICKS_PER_PERIOD_SHIFT;
82
83 from = vlib_frame_vector_args (frame);
84 n_left_from = frame->n_vectors;
85 next_index = node->cached_next_index;
86
Brian Russell1b2e16a2021-01-21 14:44:09 +000087 if (PREDICT_FALSE (pol_thread_index == ~0))
88 {
89 /*
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.
94 *
95 * This could happen simultaneously on another thread.
96 */
97 clib_atomic_cmp_and_swap (&pol->thread_index, ~0, this_thread_index);
98 pol_thread_index = this_thread_index;
99 }
100
Neale Rannsd91c1db2017-07-31 02:30:50 -0700101 while (n_left_from > 0)
102 {
103 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
104
105 while (n_left_from >= 4 && n_left_to_next >= 2)
106 {
107 vlib_buffer_t *b0, *b1;
108 u32 next0, next1;
109 u8 act0, act1;
110 u32 bi0, bi1;
111
112 next0 = next1 = 0;
113 bi0 = to_next[0] = from[0];
114 bi1 = to_next[1] = from[1];
115
116 from += 2;
117 n_left_from -= 2;
118 to_next += 2;
119 n_left_to_next -= 2;
120
121 b0 = vlib_get_buffer (vm, bi0);
122 b1 = vlib_get_buffer (vm, bi1);
123
Brian Russell1b2e16a2021-01-21 14:44:09 +0000124 if (PREDICT_FALSE (this_thread_index != pol_thread_index))
Neale Rannsd91c1db2017-07-31 02:30:50 -0700125 {
Brian Russell1b2e16a2021-01-21 14:44:09 +0000126 next0 = next1 = IP_PUNT_POLICER_NEXT_HANDOFF;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700127 }
Brian Russell1b2e16a2021-01-21 14:44:09 +0000128 else
Neale Rannsd91c1db2017-07-31 02:30:50 -0700129 {
Brian Russell1b2e16a2021-01-21 14:44:09 +0000130
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);
135
136 act0 =
137 vnet_policer_police (vm, b0, policer_index,
138 time_in_policer_periods, POLICE_CONFORM);
139 act1 =
140 vnet_policer_police (vm, b1, policer_index,
141 time_in_policer_periods, POLICE_CONFORM);
142
143 if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
144 {
145 next0 = IP_PUNT_POLICER_NEXT_DROP;
146 b0->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
147 }
148 if (PREDICT_FALSE (act1 == SSE2_QOS_ACTION_DROP))
149 {
150 next1 = IP_PUNT_POLICER_NEXT_DROP;
151 b1->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
152 }
153
154 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
155 {
156 ip_punt_policer_trace_t *t =
157 vlib_add_trace (vm, node, b0, sizeof (*t));
158 t->next = next0;
159 t->policer_index = policer_index;
160 }
161 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
162 {
163 ip_punt_policer_trace_t *t =
164 vlib_add_trace (vm, node, b1, sizeof (*t));
165 t->next = next1;
166 t->policer_index = policer_index;
167 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700168 }
169
Neale Rannsd91c1db2017-07-31 02:30:50 -0700170 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
171 n_left_to_next,
172 bi0, bi1, next0, next1);
173 }
174 while (n_left_from > 0 && n_left_to_next > 0)
175 {
176 vlib_buffer_t *b0;
177 u32 next0;
178 u32 bi0;
179 u8 act0;
180
181 next0 = 0;
182 bi0 = to_next[0] = from[0];
183
184 from += 1;
185 n_left_from -= 1;
186 to_next += 1;
187 n_left_to_next -= 1;
188
189 b0 = vlib_get_buffer (vm, bi0);
190
Brian Russell1b2e16a2021-01-21 14:44:09 +0000191 if (PREDICT_FALSE (this_thread_index != pol_thread_index))
Neale Rannsd91c1db2017-07-31 02:30:50 -0700192 {
Brian Russell1b2e16a2021-01-21 14:44:09 +0000193 next0 = IP_PUNT_POLICER_NEXT_HANDOFF;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700194 }
Brian Russell1b2e16a2021-01-21 14:44:09 +0000195 else
Neale Rannsd91c1db2017-07-31 02:30:50 -0700196 {
Brian Russell1b2e16a2021-01-21 14:44:09 +0000197 vnet_get_config_data (&cm->config_main,
198 &b0->current_config_index, &next0, 0);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700199
Brian Russell1b2e16a2021-01-21 14:44:09 +0000200 act0 =
201 vnet_policer_police (vm, b0, policer_index,
202 time_in_policer_periods, POLICE_CONFORM);
203 if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
204 {
205 next0 = IP_PUNT_POLICER_NEXT_DROP;
206 b0->error = node->errors[IP_PUNT_POLICER_ERROR_DROP];
207 }
208
209 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
210 {
211 ip_punt_policer_trace_t *t =
212 vlib_add_trace (vm, node, b0, sizeof (*t));
213 t->next = next0;
214 t->policer_index = policer_index;
215 }
216 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700217 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
218 n_left_to_next, bi0, next0);
219 }
220 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
221 }
222
223 return frame->n_vectors;
224}
225
226/**
227 * IP4 punt redirect per-rx interface configuration
228 * redirect punted traffic to another location.
229 */
230typedef struct ip_punt_redirect_rx_t_
231{
232 /**
Neale Ranns92207752019-06-03 13:21:40 +0000233 * Node linkage into the FIB graph
Neale Rannsd91c1db2017-07-31 02:30:50 -0700234 */
Neale Ranns92207752019-06-03 13:21:40 +0000235 fib_node_t node;
236
237 fib_protocol_t fproto;
238 fib_forward_chain_type_t payload_type;
239 fib_node_index_t pl;
240 u32 sibling;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700241
242 /**
Neale Ranns92207752019-06-03 13:21:40 +0000243 * redirect forwarding
Neale Rannsd91c1db2017-07-31 02:30:50 -0700244 */
Neale Ranns92207752019-06-03 13:21:40 +0000245 dpo_id_t dpo;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700246} ip_punt_redirect_rx_t;
247
248/**
249 * IP punt redirect configuration
250 */
251typedef struct ip_punt_redirect_t_
252{
Neale Ranns92207752019-06-03 13:21:40 +0000253 ip_punt_redirect_rx_t *pool;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700254
Neale Ranns92207752019-06-03 13:21:40 +0000255 /**
256 * per-RX interface configuration.
257 * sw_if_index = 0 (from which packets are never received) is used to
258 * indicate 'from-any'
259 */
260 index_t *redirect_by_rx_sw_if_index[FIB_PROTOCOL_IP_MAX];
261} ip_punt_redirect_cfg_t;
262
263extern ip_punt_redirect_cfg_t ip_punt_redirect_cfg;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700264
265/**
266 * IP punt redirect next nodes
267 */
268typedef enum ip_punt_redirect_next_t_
269{
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;
275
276/**
277 * IP Punt redirect trace
278 */
279typedef struct ip4_punt_redirect_trace_t_
280{
Neale Ranns92207752019-06-03 13:21:40 +0000281 index_t rrxi;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700282 u32 next;
283} ip_punt_redirect_trace_t;
284
285/**
286 * Add a punt redirect entry
287 */
Neale Ranns92207752019-06-03 13:21:40 +0000288extern void ip_punt_redirect_add (fib_protocol_t fproto,
Neale Rannsd91c1db2017-07-31 02:30:50 -0700289 u32 rx_sw_if_index,
Neale Ranns92207752019-06-03 13:21:40 +0000290 fib_forward_chain_type_t ct,
291 fib_route_path_t * rpaths);
292
293extern void ip_punt_redirect_del (fib_protocol_t fproto, u32 rx_sw_if_index);
294extern index_t ip_punt_redirect_find (fib_protocol_t fproto,
295 u32 rx_sw_if_index);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700296extern u8 *format_ip_punt_redirect (u8 * s, va_list * args);
297
298extern u8 *format_ip_punt_redirect_trace (u8 * s, va_list * args);
299
Neale Ranns92207752019-06-03 13:21:40 +0000300typedef 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);
303extern void ip_punt_redirect_walk (fib_protocol_t fproto,
304 ip_punt_redirect_walk_cb_t cb, void *ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +0100305
Neale Ranns92207752019-06-03 13:21:40 +0000306static_always_inline ip_punt_redirect_rx_t *
307ip_punt_redirect_get (index_t rrxi)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700308{
Neale Ranns92207752019-06-03 13:21:40 +0000309 return (pool_elt_at_index (ip_punt_redirect_cfg.pool, rrxi));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700310}
311
312always_inline uword
313ip_punt_redirect (vlib_main_t * vm,
314 vlib_node_runtime_t * node,
Neale Ranns92207752019-06-03 13:21:40 +0000315 vlib_frame_t * frame, u8 arc_index, fib_protocol_t fproto)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700316{
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];
Neale Ranns92207752019-06-03 13:21:40 +0000320 index_t *redirects;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700321
322 from = vlib_frame_vector_args (frame);
323 n_left_from = frame->n_vectors;
324 next_index = node->cached_next_index;
Neale Ranns92207752019-06-03 13:21:40 +0000325 redirects = ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto];
Neale Rannsd91c1db2017-07-31 02:30:50 -0700326
327 while (n_left_from > 0)
328 {
329 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
330
331 while (n_left_from > 0 && n_left_to_next > 0)
332 {
Neale Ranns92207752019-06-03 13:21:40 +0000333 u32 rx_sw_if_index0, rrxi0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700334 ip_punt_redirect_rx_t *rrx0;
335 vlib_buffer_t *b0;
336 u32 next0;
337 u32 bi0;
338
Neale Ranns92207752019-06-03 13:21:40 +0000339 rrxi0 = INDEX_INVALID;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700340 next0 = 0;
341 bi0 = to_next[0] = from[0];
342
343 from += 1;
344 n_left_from -= 1;
345 to_next += 1;
346 n_left_to_next -= 1;
347
348 b0 = vlib_get_buffer (vm, bi0);
349
350 vnet_get_config_data (&cm->config_main,
351 &b0->current_config_index, &next0, 0);
352
353 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
354
Neale Ranns92207752019-06-03 13:21:40 +0000355 /*
356 * If config exists for this particular RX interface use it,
357 * else use the default (at RX = 0)
358 */
359 if (vec_len (redirects) > rx_sw_if_index0)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700360 {
Neale Ranns92207752019-06-03 13:21:40 +0000361 rrxi0 = redirects[rx_sw_if_index0];
362 if (INDEX_INVALID == rrxi0)
363 rrxi0 = redirects[0];
Neale Rannsd91c1db2017-07-31 02:30:50 -0700364 }
Neale Ranns92207752019-06-03 13:21:40 +0000365 else if (vec_len (redirects) >= 1)
366 rrxi0 = redirects[0];
367
368 if (PREDICT_TRUE (INDEX_INVALID != rrxi0))
Neale Rannsd91c1db2017-07-31 02:30:50 -0700369 {
Neale Ranns92207752019-06-03 13:21:40 +0000370 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;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700373 }
374
375 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
376 {
377 ip_punt_redirect_trace_t *t =
378 vlib_add_trace (vm, node, b0, sizeof (*t));
379 t->next = next0;
Neale Ranns92207752019-06-03 13:21:40 +0000380 t->rrxi = rrxi0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700381 }
382
383 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
384 n_left_to_next, bi0, next0);
385 }
386
387 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
388 }
389
390 return frame->n_vectors;
391}
392
393always_inline uword
394ip_drop_or_punt (vlib_main_t * vm,
395 vlib_node_runtime_t * node,
396 vlib_frame_t * frame, u8 arc_index)
397{
398 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
399
400 from = vlib_frame_vector_args (frame);
401 n_left_from = frame->n_vectors;
402 next_index = node->cached_next_index;
403
404 while (n_left_from > 0)
405 {
406 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
407
408 while (n_left_from >= 8 && n_left_to_next >= 4)
409 {
410 vlib_buffer_t *b0, *b1, *b2, *b3;
411 u32 next0, next1, next2, next3;
412 u32 bi0, bi1, bi2, bi3;
413
414 next0 = next1 = next2 = next3 = 0;
415
416 /* Prefetch next iteration. */
417 {
418 vlib_buffer_t *p4, *p5, *p6, *p7;
419
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]);
424
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);
429 }
430
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];
435
436 from += 4;
437 n_left_from -= 4;
438 to_next += 4;
439 n_left_to_next -= 4;
440
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);
445
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);
452
453 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
454 to_next, n_left_to_next,
455 bi0, bi1, bi2, bi3,
456 next0, next1, next2, next3);
457 }
458
459 while (n_left_from > 0 && n_left_to_next > 0)
460 {
461 vlib_buffer_t *b0;
462 u32 next0;
463 u32 bi0;
464
465 next0 = 0;
466 bi0 = to_next[0] = from[0];
467
468 from += 1;
469 n_left_from -= 1;
470 to_next += 1;
471 n_left_to_next -= 1;
472
473 b0 = vlib_get_buffer (vm, bi0);
474
475 vnet_feature_arc_start (arc_index, 0, &next0, b0);
476
477 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
478 n_left_to_next, bi0, next0);
479 }
480 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
481 }
482
483 return frame->n_vectors;
484}
485
486#endif
487
488/*
489 * fd.io coding-style-patch-verification: ON
490 *
491 * Local Variables:
492 * eval: (c-set-style "gnu")
493 * End:
494 */