FD.io VPP  v16.06
Vector Packet Processing
l2_vtr.h
Go to the documentation of this file.
1 /*
2  * l2_vtr.h : layer 2 vlan tag rewrite processing
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef included_vnet_l2_vtr_h
19 #define included_vnet_l2_vtr_h
20 
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ethernet/packet.h>
24 #include <vnet/l2/l2_vtr.h>
25 
26 // VTR config options for API and CLI support
27 typedef enum {
37 } l2_vtr_op_t;
38 
39 // Per-interface vlan tag rewrite configuration
40 // There will be one instance of this struct for each sw_if_index
41 // for both input vtr and output vtr
42 typedef struct {
43  union {
44  // Up to two vlan tags to push.
45  // if there is only one vlan tag to push, it is in tags[1].
48  };
49 
50  union {
51  struct {
52  u8 push_bytes; // number of bytes to push for up to 2 vlans (0,4,8)
53  u8 pop_bytes; // number of bytes to pop for up to 2 vlans (0,4,8)
54  };
55  u16 push_and_pop_bytes; // if 0 then the feature is disabled
56  };
57 } vtr_config_t;
58 
59 
60 // Perform the configured tag rewrite on the packet.
61 // Return 0 if ok, 1 if packet should be dropped (e.g. tried to pop too many tags)
64  vtr_config_t * config)
65 {
66  u64 temp_8;
67  u32 temp_4;
68  u8 * eth;
69 
70  eth = vlib_buffer_get_current (b0);
71 
72  // copy the 12B dmac and smac to a temporary location
73  temp_8 = *((u64 *)eth);
74  temp_4 = *((u32 *)(eth+8));
75 
76  // adjust for popped tags
77  eth += config->pop_bytes;
78 
79  // if not enough tags to pop then drop packet
80  if (PREDICT_FALSE ((vnet_buffer(b0)->l2.l2_len - 12) < config->pop_bytes)) {
81  return 1;
82  }
83 
84  // copy the 2 new tags to the start of the packet
85  *((u64 *)(eth + 12 - 8)) = config->raw_tags;
86 
87  // TODO: set cos bits
88 
89  // adjust for pushed tags:
90  eth -= config->push_bytes;
91 
92  // copy the 12 dmac and smac back to the packet
93  *((u64 *)eth) = temp_8;
94  *((u32 *)(eth+8)) = temp_4;
95 
96  // Update l2_len
97  vnet_buffer(b0)->l2.l2_len += (word)config->push_bytes - (word)config->pop_bytes;
98 
99  // Update vlan tag count
101  (word)config->push_bytes - (word)config->pop_bytes);
102 
103  // Update packet len
104  vlib_buffer_advance(b0, (word)config->pop_bytes - (word)config->push_bytes);
105 
106  return 0;
107 }
108 
109 
110 // Perform the egress pre-vlan tag rewrite EFP Filter check. The post-vlan tag rewrite
111 // check is a separate graph node.
112 //
113 // This check insures that a packet being output to an interface (before output vtr
114 // is performed) has vlan tags that match those on a packet received from that
115 // interface (after vtr has been performed).
116 // This means verifying that any tags pushed by input vtr are present on the packet.
117 //
118 // Return 0 if ok, 1 if packet should be dropped.
119 // This function should be passed the input vtr config for the interface.
122  vtr_config_t * in_config)
123 {
124  u8 * eth;
125  u64 packet_tags;
126  u64 tag_mask;
127 
128  eth = vlib_buffer_get_current (b0);
129 
130  // If there are 2 tags pushed, they must match config->tags[0] and config->tags[1].
131  // If there is one tag pushed, it must match config->tag[1].
132  // If there are 0 tags pushed, the check passes.
133 
134  // mask for two vlan id and ethertypes, no cos bits
135  tag_mask = clib_net_to_host_u64(0xFFFF0FFFFFFF0FFF);
136  // mask for one vlan id and ethertype, no cos bits
137  tag_mask = (in_config->push_bytes == 4) ? clib_net_to_host_u64(0xFFFF0FFF) : tag_mask;
138  // mask for always match
139  tag_mask = (in_config->push_bytes == 0) ? 0 : tag_mask;
140 
141  // Read 8B from the packet, getting the proper set of vlan tags
142  // For 0 push bytes, the address doesn't matter since the mask clears the data to 0.
143  packet_tags = *((u64 *)(eth + 4 + in_config->push_bytes));
144 
145  // Check if the packet tags match the configured tags
146  return (packet_tags & tag_mask) != in_config->raw_tags;
147 }
148 
149 
150 // Configure vtag tag rewrite on the given interface.
151 // Return 1 if there is an error, 0 if ok
154  u32 sw_if_index,
155  u32 vtr_op,
156  u32 push_dot1q,
157  u32 vtr_tag1,
158  u32 vtr_tag2);
159 
160 // Get vtag tag rewrite on the given interface.
161 // Return 1 if there is an error, 0 if ok
162 u32 l2vtr_get (vlib_main_t * vlib_main,
163  vnet_main_t * vnet_main,
164  u32 sw_if_index,
165  u32 *vtr_op,
166  u32 *push_dot1q,
167  u32 *vtr_tag1,
168  u32 *vtr_tag2);
169 
170 #endif // included_vnet_l2_vtr_h
171 
u8 push_bytes
Definition: l2_vtr.h:52
u8 pop_bytes
Definition: l2_vtr.h:53
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
u16 push_and_pop_bytes
Definition: l2_vtr.h:55
#define always_inline
Definition: clib.h:84
unsigned long u64
Definition: types.h:89
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:1538
#define ethernet_buffer_adjust_vlan_count_by_bytes(b, v)
Adjusts the vlan count by the header size byte delta in &#39;v&#39;.
Definition: ethernet.h:335
#define PREDICT_FALSE(x)
Definition: clib.h:97
vnet_main_t vnet_main
Definition: misc.c:42
always_inline u32 l2_vtr_process(vlib_buffer_t *b0, vtr_config_t *config)
Definition: l2_vtr.h:63
u32 l2vtr_get(vlib_main_t *vlib_main, vnet_main_t *vnet_main, u32 sw_if_index, u32 *vtr_op, u32 *push_dot1q, u32 *vtr_tag1, u32 *vtr_tag2)
Definition: l2_vtr.c:230
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u64 raw_tags
Definition: l2_vtr.h:47
l2_vtr_op_t
Definition: l2_vtr.h:27
unsigned short u16
Definition: types.h:57
i64 word
Definition: types.h:111
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
unsigned char u8
Definition: types.h:56
u32 l2vtr_configure(vlib_main_t *vlib_main, vnet_main_t *vnet_main, u32 sw_if_index, u32 vtr_op, u32 push_dot1q, u32 vtr_tag1, u32 vtr_tag2)
Definition: l2_vtr.c:44
always_inline u8 l2_efp_filter_process(vlib_buffer_t *b0, vtr_config_t *in_config)
Definition: l2_vtr.h:121