6a846f377bad60be608536ce3b939e68b212cc29
[vpp.git] / vnet / vnet / l2 / l2_output.h
1 /*
2  * l2_output.h : layer 2 output packet 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_output_h
19 #define included_vnet_l2_output_h
20
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vnet/l2/feat_bitmap.h>
24 #include <vnet/l2/l2_vtr.h>
25
26
27 /* The L2 output feature configuration, a per-interface struct */
28 typedef struct
29 {
30
31   u32 feature_bitmap;
32
33   /*
34    * vlan tag rewrite for ingress and egress
35    * ingress vtr is located here because the same config data is used for
36    * the egress EFP filter check
37    */
38   vtr_config_t input_vtr;
39   vtr_config_t output_vtr;
40
41   /* some of these flags may get integrated into the feature bitmap */
42   u8 fwd_enable;
43   u8 flood_enable;
44
45   /* split horizon group */
46   u8 shg;
47
48 } l2_output_config_t;
49
50
51 /*
52  * The set of next nodes for features and interface output.
53  * Each output feature node should include this.
54  */
55 typedef struct
56 {
57   /*
58    * vector of output next node index, indexed by sw_if_index.
59    * used when all output features have been executed and the
60    * next nodes are the interface output nodes.
61    */
62   u32 *output_node_index_vec;
63
64   /*
65    * array of next node index for each output feature, indexed
66    * by l2output_feat_t. Used to determine next feature node.
67    */
68   u32 feat_next_node_index[32];
69
70 } l2_output_next_nodes_st;
71
72
73 typedef struct
74 {
75   /* Next nodes for features and output interfaces */
76   l2_output_next_nodes_st next_nodes;
77
78   /* config vector indexed by sw_if_index */
79   l2_output_config_t *configs;
80
81   /* Convenience variables */
82   vlib_main_t *vlib_main;
83   vnet_main_t *vnet_main;
84 } l2output_main_t;
85
86 l2output_main_t l2output_main;
87
88 /* L2 output features */
89
90 /* Mappings from feature ID to graph node name */
91 #define foreach_l2output_feat \
92  _(SPAN,              "feature-bitmap-drop")        \
93  _(CFM,               "feature-bitmap-drop")        \
94  _(QOS,               "feature-bitmap-drop")        \
95  _(ACL,               "l2-output-acl")              \
96  _(L2PT,              "feature-bitmap-drop")        \
97  _(EFP_FILTER,        "l2-efp-filter")              \
98  _(IPIW,              "feature-bitmap-drop")        \
99  _(STP_BLOCKED,       "feature-bitmap-drop")        \
100  _(LINESTATUS_DOWN,   "feature-bitmap-drop")        \
101  _(OUTPUT_CLASSIFY,   "l2-output-classify")         \
102  _(XCRW,              "l2-xcrw")
103
104 /* Feature bitmap positions */
105 typedef enum
106 {
107 #define _(sym,str) L2OUTPUT_FEAT_##sym##_BIT,
108   foreach_l2output_feat
109 #undef _
110     L2OUTPUT_N_FEAT,
111 } l2output_feat_t;
112
113 /* Feature bit masks */
114 typedef enum
115 {
116 #define _(sym,str) L2OUTPUT_FEAT_##sym = (1<<L2OUTPUT_FEAT_##sym##_BIT),
117   foreach_l2output_feat
118 #undef _
119 } l2output_feat_masks_t;
120
121 #define foreach_l2output_error                          \
122 _(L2OUTPUT,     "L2 output packets")                    \
123 _(EFP_DROP,     "L2 EFP filter pre-rewrite drops")      \
124 _(VTR_DROP,     "L2 output tag rewrite drops")          \
125 _(SHG_DROP,     "L2 split horizon drops")               \
126 _(DROP,         "L2 output drops")                      \
127 _(MAPPING_DROP, "L2 Output interface mapping in progress")
128
129 typedef enum
130 {
131   L2OUTPUT_NEXT_DROP,
132   L2OUTPUT_NEXT_DEL_TUNNEL,
133   L2OUTPUT_N_NEXT,
134 } l2output_next_t;
135
136 typedef enum
137 {
138 #define _(sym,str) L2OUTPUT_ERROR_##sym,
139   foreach_l2output_error
140 #undef _
141     L2OUTPUT_N_ERROR,
142 } l2output_error_t;
143
144 /* Return an array of strings containing graph node names of each feature */
145 char **l2output_get_feat_names (void);
146
147
148 /**
149  * The next set of functions is for use by output feature graph nodes.
150  * When the last bit has been cleared from the output feature bitmap,
151  * the next node is the output graph node for the TX sw_if_index.
152  * These functions help the feature nodes get that node index.
153  */
154
155 /* Create a mapping to the output graph node for the given sw_if_index */
156 u32 l2output_create_output_node_mapping (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 node_index,      /* index of current node */
157                                          u32 * output_node_index_vec,
158                                          u32 sw_if_index);
159
160 /* Initialize the next node mapping table */
161 always_inline void
162 l2output_init_output_node_vec (u32 ** output_node_index_vec)
163 {
164
165   /*
166    * Size it at 100 sw_if_indexes initially
167    * Uninitialized mappings are set to ~0
168    */
169   vec_validate_init_empty (*output_node_index_vec, 100, ~0);
170 }
171
172
173 /**
174  *  Get a mapping from the output node mapping table,
175  * creating the entry if necessary.
176  */
177 always_inline u32
178 l2output_get_output_node (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 node_index,     /* index of current node */
179                           u32 sw_if_index, u32 ** output_node_index_vec)        /* may be updated */
180 {
181   u32 next;                     /* index of next graph node */
182
183   /* Insure the vector is big enough */
184   vec_validate_init_empty (*output_node_index_vec, sw_if_index, ~0);
185
186   /* Get the mapping for the sw_if_index */
187   next = vec_elt (*output_node_index_vec, sw_if_index);
188
189   if (next == ~0)
190     {
191       /* Mapping doesn't exist so create it */
192       next = l2output_create_output_node_mapping (vlib_main,
193                                                   vnet_main,
194                                                   node_index,
195                                                   *output_node_index_vec,
196                                                   sw_if_index);
197     }
198
199   return next;
200 }
201
202
203 /** Determine the next L2 node based on the output feature bitmap */
204 always_inline void
205 l2_output_dispatch (vlib_main_t * vlib_main,
206                     vnet_main_t * vnet_main,
207                     vlib_node_runtime_t * node,
208                     u32 node_index,
209                     u32 * cached_sw_if_index,
210                     u32 * cached_next_index,
211                     l2_output_next_nodes_st * next_nodes,
212                     vlib_buffer_t * b0,
213                     u32 sw_if_index, u32 feature_bitmap, u32 * next0)
214 {
215   if (feature_bitmap)
216     {
217       /* There are some features to execute */
218
219       /* Save bitmap for the next feature graph nodes */
220       vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
221
222       /* Determine the next node */
223       *next0 =
224         feat_bitmap_get_next_node_index (next_nodes->feat_next_node_index,
225                                          feature_bitmap);
226     }
227   else
228     {
229       /*
230        * There are no features. Send packet to TX node for sw_if_index0
231        * This is a little tricky in that the output interface next node indexes
232        * are not precomputed at init time.
233        */
234
235       if (sw_if_index == *cached_sw_if_index)
236         {
237           /* We hit in the one-entry cache. Use it. */
238           *next0 = *cached_next_index;
239         }
240       else
241         {
242           /* Look up the output TX node */
243           *next0 = l2output_get_output_node (vlib_main,
244                                              vnet_main,
245                                              node_index,
246                                              sw_if_index,
247                                              &next_nodes->output_node_index_vec);
248
249           if (*next0 == L2OUTPUT_NEXT_DROP)
250             {
251               vnet_hw_interface_t *hw0;
252               hw0 = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
253
254               if (hw0->flags & VNET_HW_INTERFACE_FLAG_L2OUTPUT_MAPPED)
255                 b0->error = node->errors[L2OUTPUT_ERROR_MAPPING_DROP];
256             }
257
258           /* Update the one-entry cache */
259           *cached_sw_if_index = sw_if_index;
260           *cached_next_index = *next0;
261         }
262     }
263 }
264
265 /** Get a pointer to the config for the given interface */
266 l2_output_config_t *l2output_intf_config (u32 sw_if_index);
267
268 /** Enable (or disable) the feature in the bitmap for the given interface */
269 void l2output_intf_bitmap_enable (u32 sw_if_index,
270                                   u32 feature_bitmap, u32 enable);
271
272 #endif
273
274 /*
275  * fd.io coding-style-patch-verification: ON
276  *
277  * Local Variables:
278  * eval: (c-set-style "gnu")
279  * End:
280  */