gbp: migrate old MULTIARCH macros to VLIB_NODE_FN
[vpp.git] / src / plugins / gbp / gbp_vxlan_node.c
1 /*
2  * Copyright (c) 2019 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 #include <plugins/gbp/gbp_vxlan.h>
16 #include <plugins/gbp/gbp_itf.h>
17 #include <plugins/gbp/gbp_learn.h>
18 #include <plugins/gbp/gbp_bridge_domain.h>
19 #include <plugins/gbp/gbp_route_domain.h>
20 #include <plugins/gbp/gbp_sclass.h>
21
22 #include <vnet/vxlan-gbp/vxlan_gbp.h>
23 #include <vlibmemory/api.h>
24 #include <vnet/fib/fib_table.h>
25
26 extern uword *gv_db;
27
28 typedef struct gbp_vxlan_trace_t_
29 {
30   u8 dropped;
31   u32 vni;
32   u32 sw_if_index;
33   u16 sclass;
34   u8 flags;
35 } gbp_vxlan_trace_t;
36
37 #define foreach_gbp_vxlan_input_next         \
38   _(DROP, "error-drop")                      \
39   _(L2_INPUT, "l2-input")                    \
40   _(IP4_INPUT, "ip4-input")                  \
41   _(IP6_INPUT, "ip6-input")
42
43 typedef enum
44 {
45 #define _(s,n) GBP_VXLAN_INPUT_NEXT_##s,
46   foreach_gbp_vxlan_input_next
47 #undef _
48     GBP_VXLAN_INPUT_N_NEXT,
49 } gbp_vxlan_input_next_t;
50
51
52 #define foreach_gbp_vxlan_error              \
53   _(DECAPPED, "decapped")                    \
54   _(LEARNED, "learned")
55
56 typedef enum
57 {
58 #define _(s,n) GBP_VXLAN_ERROR_##s,
59   foreach_gbp_vxlan_error
60 #undef _
61     GBP_VXLAN_N_ERROR,
62 } gbp_vxlan_input_error_t;
63
64 static char *gbp_vxlan_error_strings[] = {
65 #define _(n,s) s,
66   foreach_gbp_vxlan_error
67 #undef _
68 };
69
70 static uword
71 gbp_vxlan_decap (vlib_main_t * vm,
72                  vlib_node_runtime_t * node,
73                  vlib_frame_t * from_frame, u8 is_ip4)
74 {
75   u32 n_left_to_next, n_left_from, next_index, *to_next, *from;
76
77   next_index = 0;
78   from = vlib_frame_vector_args (from_frame);
79   n_left_from = from_frame->n_vectors;
80
81   while (n_left_from > 0)
82     {
83
84       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
85
86       while (n_left_from > 0 && n_left_to_next > 0)
87         {
88           vxlan_gbp_header_t *vxlan_gbp0;
89           gbp_vxlan_input_next_t next0;
90           gbp_vxlan_tunnel_t *gt0;
91           vlib_buffer_t *b0;
92           u32 bi0, vni0;
93           uword *p;
94
95           bi0 = to_next[0] = from[0];
96           from += 1;
97           to_next += 1;
98           n_left_from -= 1;
99           n_left_to_next -= 1;
100           next0 = GBP_VXLAN_INPUT_NEXT_DROP;
101
102           b0 = vlib_get_buffer (vm, bi0);
103           vxlan_gbp0 =
104             vlib_buffer_get_current (b0) - sizeof (vxlan_gbp_header_t);
105
106           vni0 = vxlan_gbp_get_vni (vxlan_gbp0);
107           p = hash_get (gv_db, vni0);
108
109           if (PREDICT_FALSE (NULL == p))
110             {
111               gt0 = NULL;
112               next0 = GBP_VXLAN_INPUT_NEXT_DROP;
113             }
114           else
115             {
116               gt0 = gbp_vxlan_tunnel_get (p[0]);
117
118               vnet_buffer (b0)->sw_if_index[VLIB_RX] = gt0->gt_sw_if_index;
119
120               if (GBP_VXLAN_TUN_L2 == gt0->gt_layer)
121                 /*
122                  * An L2 layer tunnel goes into the BD
123                  */
124                 next0 = GBP_VXLAN_INPUT_NEXT_L2_INPUT;
125               else
126                 {
127                   /*
128                    * An L3 layer tunnel needs to strip the L2 header
129                    * an inject into the RD
130                    */
131                   ethernet_header_t *e0;
132                   u16 type0;
133
134                   e0 = vlib_buffer_get_current (b0);
135                   type0 = clib_net_to_host_u16 (e0->type);
136                   switch (type0)
137                     {
138                     case ETHERNET_TYPE_IP4:
139                       next0 = GBP_VXLAN_INPUT_NEXT_IP4_INPUT;
140                       break;
141                     case ETHERNET_TYPE_IP6:
142                       next0 = GBP_VXLAN_INPUT_NEXT_IP6_INPUT;
143                       break;
144                     default:
145                       goto trace;
146                     }
147                   vlib_buffer_advance (b0, sizeof (*e0));
148                 }
149             }
150
151         trace:
152           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
153             {
154               gbp_vxlan_trace_t *tr;
155
156               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
157               tr->dropped = (next0 == GBP_VXLAN_INPUT_NEXT_DROP);
158               tr->vni = vni0;
159               tr->sw_if_index = (gt0 ? gt0->gt_sw_if_index : ~0);
160               tr->flags = vxlan_gbp_get_gpflags (vxlan_gbp0);
161               tr->sclass = vxlan_gbp_get_sclass (vxlan_gbp0);
162             }
163
164           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
165                                            to_next, n_left_to_next,
166                                            bi0, next0);
167         }
168
169       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
170     }
171
172   return from_frame->n_vectors;
173 }
174
175 VLIB_NODE_FN (gbp_vxlan4_input_node) (vlib_main_t * vm,
176                                       vlib_node_runtime_t * node,
177                                       vlib_frame_t * from_frame)
178 {
179   return gbp_vxlan_decap (vm, node, from_frame, 1);
180 }
181
182 static u8 *
183 format_gbp_vxlan_rx_trace (u8 * s, va_list * args)
184 {
185   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
186   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
187   gbp_vxlan_trace_t *t = va_arg (*args, gbp_vxlan_trace_t *);
188
189   s = format (s, "vni:%d dropped:%d rx:%d sclass:%d flags:%U",
190               t->vni, t->dropped, t->sw_if_index,
191               t->sclass, format_vxlan_gbp_header_gpflags, t->flags);
192
193   return (s);
194 }
195
196 /* *INDENT-OFF* */
197 VLIB_REGISTER_NODE (gbp_vxlan4_input_node) =
198 {
199   .name = "gbp-vxlan4",
200   .vector_size = sizeof (u32),
201   .n_errors = GBP_VXLAN_N_ERROR,
202   .error_strings = gbp_vxlan_error_strings,
203   .n_next_nodes = GBP_VXLAN_INPUT_N_NEXT,
204   .format_trace = format_gbp_vxlan_rx_trace,
205   .next_nodes = {
206 #define _(s,n) [GBP_VXLAN_INPUT_NEXT_##s] = n,
207     foreach_gbp_vxlan_input_next
208 #undef _
209   },
210 };
211 /* *INDENT-ON* */
212
213 /*
214  * fd.io coding-style-patch-verification: ON
215  *
216  * Local Variables:
217  * eval: (c-set-style "gnu")
218  * End:
219  */