blob: 59afb5625090ed3af0662e15d6143d5b0ebfc6ac [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#ifndef __POLICE_INLINES_H__
16#define __POLICE_INLINES_H__
17
18#include <vnet/policer/police.h>
19#include <vnet/vnet.h>
20#include <vnet/ip/ip.h>
21
22#define IP4_NON_DSCP_BITS 0x03
23#define IP4_DSCP_SHIFT 2
24#define IP6_NON_DSCP_BITS 0xf03fffff
25#define IP6_DSCP_SHIFT 22
26
27static_always_inline void
Brian Russelle3845d72021-02-08 15:33:18 +000028vnet_policer_mark (vlib_buffer_t *b, ip_dscp_t dscp)
Neale Rannsd91c1db2017-07-31 02:30:50 -070029{
30 ethernet_header_t *eh;
31 ip4_header_t *ip4h;
32 ip6_header_t *ip6h;
33 u16 type;
34
35 eh = (ethernet_header_t *) b->data;
36 type = clib_net_to_host_u16 (eh->type);
37
38 if (PREDICT_TRUE (type == ETHERNET_TYPE_IP4))
39 {
40 ip4h = (ip4_header_t *) & (b->data[sizeof (ethernet_header_t)]);;
41 ip4h->tos &= IP4_NON_DSCP_BITS;
42 ip4h->tos |= dscp << IP4_DSCP_SHIFT;
43 ip4h->checksum = ip4_header_checksum (ip4h);
44 }
45 else
46 {
47 if (PREDICT_TRUE (type == ETHERNET_TYPE_IP6))
48 {
49 ip6h = (ip6_header_t *) & (b->data[sizeof (ethernet_header_t)]);
50 ip6h->ip_version_traffic_class_and_flow_label &=
51 clib_host_to_net_u32 (IP6_NON_DSCP_BITS);
52 ip6h->ip_version_traffic_class_and_flow_label |=
53 clib_host_to_net_u32 (dscp << IP6_DSCP_SHIFT);
54 }
55 }
56}
57
58static_always_inline u8
Brian Russellfce88652021-02-18 10:25:23 +000059vnet_policer_police (vlib_main_t *vm, vlib_buffer_t *b, u32 policer_index,
Neale Rannsd91c1db2017-07-31 02:30:50 -070060 u64 time_in_policer_periods,
Brian Russellfce88652021-02-18 10:25:23 +000061 policer_result_e packet_color, bool handoff)
Neale Rannsd91c1db2017-07-31 02:30:50 -070062{
Brian Russellb2aae752021-02-09 11:36:31 +000063 qos_action_type_en act;
Neale Rannsd91c1db2017-07-31 02:30:50 -070064 u32 len;
65 u32 col;
Brian Russell54be0cc2021-02-15 11:49:42 +000066 policer_t *pol;
Neale Rannsd91c1db2017-07-31 02:30:50 -070067 vnet_policer_main_t *pm = &vnet_policer_main;
68
Brian Russella71ed782021-01-27 11:34:33 +000069 /* Speculative prefetch assuming a conform result */
70 vlib_prefetch_combined_counter (&policer_counters[POLICE_CONFORM],
71 vm->thread_index, policer_index);
72
Neale Rannsd91c1db2017-07-31 02:30:50 -070073 pol = &pm->policers[policer_index];
Brian Russellfce88652021-02-18 10:25:23 +000074
75 if (handoff)
76 {
77 if (PREDICT_FALSE (pol->thread_index == ~0))
78 /*
79 * This is the first packet to use this policer. Set the
80 * thread index in the policer to this thread and any
81 * packets seen by this node on other threads will
82 * be handed off to this one.
83 *
84 * This could happen simultaneously on another thread.
85 */
86 clib_atomic_cmp_and_swap (&pol->thread_index, ~0, vm->thread_index);
87 else if (PREDICT_FALSE (pol->thread_index != vm->thread_index))
88 return QOS_ACTION_HANDOFF;
89 }
90
91 len = vlib_buffer_length_in_chain (vm, b);
Neale Rannsd91c1db2017-07-31 02:30:50 -070092 col = vnet_police_packet (pol, len, packet_color, time_in_policer_periods);
93 act = pol->action[col];
Brian Russella71ed782021-01-27 11:34:33 +000094 vlib_increment_combined_counter (&policer_counters[col], vm->thread_index,
95 policer_index, 1, len);
Brian Russellc5299ff2021-02-09 10:16:58 +000096 if (PREDICT_TRUE (act == QOS_ACTION_MARK_AND_TRANSMIT))
Neale Rannsd91c1db2017-07-31 02:30:50 -070097 vnet_policer_mark (b, pol->mark_dscp[col]);
98
99 return act;
100}
101
Brian Russell97de8a22021-01-19 16:50:56 +0000102typedef enum
103{
104 POLICER_HANDOFF_ERROR_CONGESTION_DROP,
105} policer_handoff_error_t;
106
107typedef struct policer_handoff_trace_t_
108{
109 u32 policer_index;
110 u32 current_worker_index;
111 u32 next_worker_index;
112} policer_handoff_trace_t;
113
114extern u8 *format_policer_handoff_trace (u8 *s, va_list *args);
115
116/* Do worker handoff based on the policer's thread_index */
117static_always_inline uword
118policer_handoff (vlib_main_t *vm, vlib_node_runtime_t *node,
119 vlib_frame_t *frame, u32 fq_index, u32 policer_index)
120{
121 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
122 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
123 u32 n_enq, n_left_from, *from;
124 vnet_policer_main_t *pm;
Brian Russell54be0cc2021-02-15 11:49:42 +0000125 policer_t *policer;
Brian Russell97de8a22021-01-19 16:50:56 +0000126 u32 this_thread, policer_thread;
Brian Russell1aa99122021-02-18 11:00:38 +0000127 bool single_policer_node = (policer_index != ~0);
Brian Russell97de8a22021-01-19 16:50:56 +0000128
129 pm = &vnet_policer_main;
Brian Russell1aa99122021-02-18 11:00:38 +0000130 if (single_policer_node)
131 {
132 policer = &pm->policers[policer_index];
133 policer_thread = policer->thread_index;
134 }
Brian Russell97de8a22021-01-19 16:50:56 +0000135
136 this_thread = vm->thread_index;
137 from = vlib_frame_vector_args (frame);
138 n_left_from = frame->n_vectors;
139 vlib_get_buffers (vm, from, bufs, n_left_from);
140
141 b = bufs;
142 ti = thread_indices;
143
144 while (n_left_from > 0)
145 {
Brian Russell1aa99122021-02-18 11:00:38 +0000146 if (!single_policer_node)
147 {
148 policer_index = vnet_buffer (b[0])->policer.index;
149 policer = &pm->policers[policer_index];
150 ti[0] = policer->thread_index;
151 }
152 else
153 {
154 ti[0] = policer_thread;
155 }
Brian Russell97de8a22021-01-19 16:50:56 +0000156
157 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
158 b[0]->flags & VLIB_BUFFER_IS_TRACED))
159 {
160 policer_handoff_trace_t *t =
161 vlib_add_trace (vm, node, b[0], sizeof (*t));
162 t->current_worker_index = this_thread;
Brian Russell1aa99122021-02-18 11:00:38 +0000163 t->next_worker_index = ti[0];
Brian Russell97de8a22021-01-19 16:50:56 +0000164 t->policer_index = policer_index;
165 }
166
167 n_left_from--;
168 ti++;
169 b++;
170 }
171
172 n_enq = vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
173 frame->n_vectors, 1);
174
175 if (n_enq < frame->n_vectors)
176 vlib_node_increment_counter (vm, node->node_index,
177 POLICER_HANDOFF_ERROR_CONGESTION_DROP,
178 frame->n_vectors - n_enq);
179
180 return n_enq;
181}
Neale Rannsd91c1db2017-07-31 02:30:50 -0700182#endif // __POLICE_INLINES_H__
183
184/*
185 * fd.io coding-style-patch-verification: ON
186 *
187 * Local Variables:
188 * eval: (c-set-style "gnu")
189 * End:
190 */