FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
police_inlines.h
Go to the documentation of this file.
1 /*
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 
29 {
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)]);
51  clib_host_to_net_u32 (IP6_NON_DSCP_BITS);
53  clib_host_to_net_u32 (dscp << IP6_DSCP_SHIFT);
54  }
55  }
56 }
57 
60  u64 time_in_policer_periods,
61  policer_result_e packet_color, bool handoff)
62 {
63  qos_action_type_en act;
64  u32 len;
65  u32 col;
66  policer_t *pol;
68 
69  /* Speculative prefetch assuming a conform result */
71  vm->thread_index, policer_index);
72 
73  pol = &pm->policers[policer_index];
74 
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  */
87  else if (PREDICT_FALSE (pol->thread_index != vm->thread_index))
88  return QOS_ACTION_HANDOFF;
89  }
90 
92  col = vnet_police_packet (pol, len, packet_color, time_in_policer_periods);
93  act = pol->action[col];
95  policer_index, 1, len);
97  vnet_policer_mark (b, pol->mark_dscp[col]);
98 
99  return act;
100 }
101 
102 typedef enum
103 {
106 
108 {
113 
114 extern u8 *format_policer_handoff_trace (u8 *s, va_list *args);
115 
116 /* Do worker handoff based on the policer's thread_index */
119  vlib_frame_t *frame, u32 fq_index, u32 policer_index)
120 {
122  u16 thread_indices[VLIB_FRAME_SIZE], *ti;
123  u32 n_enq, n_left_from, *from;
125  policer_t *policer;
126  u32 this_thread, policer_thread;
127  bool single_policer_node = (policer_index != ~0);
128 
129  pm = &vnet_policer_main;
130  if (single_policer_node)
131  {
132  policer = &pm->policers[policer_index];
133  policer_thread = policer->thread_index;
134  }
135 
136  this_thread = vm->thread_index;
138  n_left_from = frame->n_vectors;
140 
141  b = bufs;
142  ti = thread_indices;
143 
144  while (n_left_from > 0)
145  {
146  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  }
156 
157  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
158  b[0]->flags & VLIB_BUFFER_IS_TRACED))
159  {
161  vlib_add_trace (vm, node, b[0], sizeof (*t));
162  t->current_worker_index = this_thread;
163  t->next_worker_index = ti[0];
164  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, node, fq_index, from,
173  thread_indices, frame->n_vectors, 1);
174 
175  if (n_enq < frame->n_vectors)
176  vlib_node_increment_counter (vm, node->node_index,
178  frame->n_vectors - n_enq);
179 
180  return n_enq;
181 }
182 #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  */
vnet_policer_main_t
Definition: policer.h:26
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vnet_police_packet
static policer_result_e vnet_police_packet(policer_t *policer, u32 packet_length, policer_result_e packet_color, u64 time)
Definition: police.h:106
ip_dscp_t
enum ip_dscp_t_ ip_dscp_t
vlib_buffer_enqueue_to_thread
static_always_inline u32 vlib_buffer_enqueue_to_thread(vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index, u32 *buffer_indices, u16 *thread_indices, u32 n_packets, int drop_on_congestion)
Definition: buffer_node.h:383
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
vnet_policer_main
vnet_policer_main_t vnet_policer_main
Definition: policer.c:22
policer_handoff_trace_t
struct policer_handoff_trace_t_ policer_handoff_trace_t
policer_handoff_error_t
policer_handoff_error_t
Definition: police_inlines.h:102
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
policer_handoff_trace_t_
Definition: police_inlines.h:107
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
policer_t
Definition: police.h:73
IP6_NON_DSCP_BITS
#define IP6_NON_DSCP_BITS
Definition: police_inlines.h:24
u16
unsigned short u16
Definition: types.h:57
ethernet_header_t::type
u16 type
Definition: packet.h:59
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
IP4_NON_DSCP_BITS
#define IP4_NON_DSCP_BITS
Definition: police_inlines.h:22
policer_t::action
qos_action_type_en action[3]
Definition: police.h:81
policer_result_e
policer_result_e
Definition: police.h:18
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
ip4_header_t
Definition: ip4_packet.h:87
policer_handoff
static_always_inline uword policer_handoff(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u32 fq_index, u32 policer_index)
Definition: police_inlines.h:118
ip4_header_t::tos
ip_dscp_t tos
Definition: ip4_packet.h:96
POLICER_HANDOFF_ERROR_CONGESTION_DROP
@ POLICER_HANDOFF_ERROR_CONGESTION_DROP
Definition: police_inlines.h:104
policer_handoff_trace_t_::current_worker_index
u32 current_worker_index
Definition: police_inlines.h:110
policer_counters
vlib_combined_counter_main_t policer_counters[]
Definition: policer.c:37
ti
u32 ti
Definition: interface_output.c:425
len
u8 len
Definition: ip_types.api:103
policer_handoff_trace_t_::policer_index
u32 policer_index
Definition: police_inlines.h:109
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
format_policer_handoff_trace
u8 * format_policer_handoff_trace(u8 *s, va_list *args)
Definition: policer.c:25
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
vlib_prefetch_combined_counter
static void vlib_prefetch_combined_counter(const vlib_combined_counter_main_t *cm, u32 thread_index, u32 index)
Pre-fetch a per-thread combined counter for the given object index.
Definition: counter.h:248
static_always_inline
#define static_always_inline
Definition: clib.h:112
policer_t::thread_index
u32 thread_index
Definition: police.h:98
uword
u64 uword
Definition: types.h:112
ethernet_header_t
Definition: packet.h:52
QOS_ACTION_MARK_AND_TRANSMIT
@ QOS_ACTION_MARK_AND_TRANSMIT
Definition: police.h:31
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:215
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
ip4_header_t::checksum
u16 checksum
Definition: ip4_packet.h:118
vnet_policer_police
static_always_inline u8 vnet_policer_police(vlib_main_t *vm, vlib_buffer_t *b, u32 policer_index, u64 time_in_policer_periods, policer_result_e packet_color, bool handoff)
Definition: police_inlines.h:59
IP4_DSCP_SHIFT
#define IP4_DSCP_SHIFT
Definition: police_inlines.h:23
u64
unsigned long u64
Definition: types.h:89
IP6_DSCP_SHIFT
#define IP6_DSCP_SHIFT
Definition: police_inlines.h:25
police.h
ip.h
POLICE_CONFORM
@ POLICE_CONFORM
Definition: police.h:20
u32
unsigned int u32
Definition: types.h:88
clib_atomic_cmp_and_swap
#define clib_atomic_cmp_and_swap(addr, old, new)
Definition: atomics.h:37
policer_handoff_trace_t_::next_worker_index
u32 next_worker_index
Definition: police_inlines.h:111
n_vectors
return frame n_vectors
Definition: nat44_ei_hairpinning.c:488
ip6_header_t
Definition: ip6_packet.h:294
vlib_main_t
Definition: main.h:102
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
ip4_header_checksum
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
QOS_ACTION_HANDOFF
@ QOS_ACTION_HANDOFF
Definition: police.h:32
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
policer_t::mark_dscp
ip_dscp_t mark_dscp[3]
Definition: police.h:82
vnet.h
vlib_node_runtime_t
Definition: node.h:454
vnet_policer_main_t::policers
policer_t * policers
Definition: policer.h:29
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
ip6_header_t::ip_version_traffic_class_and_flow_label
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:297
dscp
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vnet_policer_mark
static_always_inline void vnet_policer_mark(vlib_buffer_t *b, ip_dscp_t dscp)
Definition: police_inlines.h:28
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111