policer: remove SSE2 prefix
[vpp.git] / src / vnet / policer / police_inlines.h
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
27 static_always_inline void
28 vnet_policer_mark (vlib_buffer_t *b, ip_dscp_t dscp)
29 {
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
58 static_always_inline u8
59 vnet_policer_police (vlib_main_t * vm,
60                      vlib_buffer_t * b,
61                      u32 policer_index,
62                      u64 time_in_policer_periods,
63                      policer_result_e packet_color)
64 {
65   u8 act;
66   u32 len;
67   u32 col;
68   policer_read_response_type_st *pol;
69   vnet_policer_main_t *pm = &vnet_policer_main;
70
71   /* Speculative prefetch assuming a conform result */
72   vlib_prefetch_combined_counter (&policer_counters[POLICE_CONFORM],
73                                   vm->thread_index, policer_index);
74
75   len = vlib_buffer_length_in_chain (vm, b);
76   pol = &pm->policers[policer_index];
77   col = vnet_police_packet (pol, len, packet_color, time_in_policer_periods);
78   act = pol->action[col];
79   vlib_increment_combined_counter (&policer_counters[col], vm->thread_index,
80                                    policer_index, 1, len);
81   if (PREDICT_TRUE (act == QOS_ACTION_MARK_AND_TRANSMIT))
82     vnet_policer_mark (b, pol->mark_dscp[col]);
83
84   return act;
85 }
86
87 typedef enum
88 {
89   POLICER_HANDOFF_ERROR_CONGESTION_DROP,
90 } policer_handoff_error_t;
91
92 typedef struct policer_handoff_trace_t_
93 {
94   u32 policer_index;
95   u32 current_worker_index;
96   u32 next_worker_index;
97 } policer_handoff_trace_t;
98
99 extern u8 *format_policer_handoff_trace (u8 *s, va_list *args);
100
101 /* Do worker handoff based on the policer's thread_index */
102 static_always_inline uword
103 policer_handoff (vlib_main_t *vm, vlib_node_runtime_t *node,
104                  vlib_frame_t *frame, u32 fq_index, u32 policer_index)
105 {
106   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
107   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
108   u32 n_enq, n_left_from, *from;
109   vnet_policer_main_t *pm;
110   policer_read_response_type_st *policer;
111   u32 this_thread, policer_thread;
112
113   pm = &vnet_policer_main;
114   policer = &pm->policers[policer_index];
115   policer_thread = policer->thread_index;
116
117   this_thread = vm->thread_index;
118   from = vlib_frame_vector_args (frame);
119   n_left_from = frame->n_vectors;
120   vlib_get_buffers (vm, from, bufs, n_left_from);
121
122   b = bufs;
123   ti = thread_indices;
124
125   while (n_left_from > 0)
126     {
127       ti[0] = policer_thread;
128
129       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
130                          b[0]->flags & VLIB_BUFFER_IS_TRACED))
131         {
132           policer_handoff_trace_t *t =
133             vlib_add_trace (vm, node, b[0], sizeof (*t));
134           t->current_worker_index = this_thread;
135           t->next_worker_index = policer_thread;
136           t->policer_index = policer_index;
137         }
138
139       n_left_from--;
140       ti++;
141       b++;
142     }
143
144   n_enq = vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
145                                          frame->n_vectors, 1);
146
147   if (n_enq < frame->n_vectors)
148     vlib_node_increment_counter (vm, node->node_index,
149                                  POLICER_HANDOFF_ERROR_CONGESTION_DROP,
150                                  frame->n_vectors - n_enq);
151
152   return n_enq;
153 }
154 #endif // __POLICE_INLINES_H__
155
156 /*
157  * fd.io coding-style-patch-verification: ON
158  *
159  * Local Variables:
160  * eval: (c-set-style "gnu")
161  * End:
162  */