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.
15 #ifndef __POLICE_INLINES_H__
16 #define __POLICE_INLINES_H__
18 #include <vnet/policer/police.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip.h>
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
27 static_always_inline void
28 vnet_policer_mark (vlib_buffer_t * b, u8 dscp)
30 ethernet_header_t *eh;
35 eh = (ethernet_header_t *) b->data;
36 type = clib_net_to_host_u16 (eh->type);
38 if (PREDICT_TRUE (type == ETHERNET_TYPE_IP4))
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);
47 if (PREDICT_TRUE (type == ETHERNET_TYPE_IP6))
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);
58 static_always_inline u8
59 vnet_policer_police (vlib_main_t * vm,
62 u64 time_in_policer_periods,
63 policer_result_e packet_color)
68 policer_read_response_type_st *pol;
69 vnet_policer_main_t *pm = &vnet_policer_main;
71 len = vlib_buffer_length_in_chain (vm, b);
72 pol = &pm->policers[policer_index];
73 col = vnet_police_packet (pol, len, packet_color, time_in_policer_periods);
74 act = pol->action[col];
75 if (PREDICT_TRUE (act == SSE2_QOS_ACTION_MARK_AND_TRANSMIT))
76 vnet_policer_mark (b, pol->mark_dscp[col]);
81 #endif // __POLICE_INLINES_H__
84 * fd.io coding-style-patch-verification: ON
87 * eval: (c-set-style "gnu")