gbp: migrate old MULTIARCH macros to VLIB_NODE_FN
[vpp.git] / src / plugins / gbp / gbp_fwd_node.c
1 /*
2  * Copyright (c) 2018 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
16 #include <plugins/gbp/gbp.h>
17 #include <vnet/l2/l2_input.h>
18
19 #define foreach_gbp_fwd                      \
20   _(DROP,    "drop")                         \
21   _(OUTPUT,  "output")
22
23 typedef enum
24 {
25 #define _(sym,str) GBP_FWD_ERROR_##sym,
26   foreach_gbp_fwd
27 #undef _
28     GBP_FWD_N_ERROR,
29 } gbp_fwd_error_t;
30
31 static char *gbp_fwd_error_strings[] = {
32 #define _(sym,string) string,
33   foreach_gbp_fwd
34 #undef _
35 };
36
37 typedef enum
38 {
39 #define _(sym,str) GBP_FWD_NEXT_##sym,
40   foreach_gbp_fwd
41 #undef _
42     GBP_FWD_N_NEXT,
43 } gbp_fwd_next_t;
44
45 /**
46  * per-packet trace data
47  */
48 typedef struct gbp_fwd_trace_t_
49 {
50   /* per-pkt trace data */
51   epg_id_t src_epg;
52   u32 sw_if_index;
53 } gbp_fwd_trace_t;
54
55 VLIB_NODE_FN (gbp_fwd_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
56                              vlib_frame_t * frame)
57 {
58   u32 n_left_from, *from, *to_next;
59   u32 next_index;
60
61   next_index = 0;
62   n_left_from = frame->n_vectors;
63   from = vlib_frame_vector_args (frame);
64
65   while (n_left_from > 0)
66     {
67       u32 n_left_to_next;
68
69       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
70
71       while (n_left_from > 0 && n_left_to_next > 0)
72         {
73           u32 bi0, sw_if_index0, src_epg;
74           gbp_fwd_next_t next0;
75           vlib_buffer_t *b0;
76
77           next0 = GBP_FWD_NEXT_DROP;
78           bi0 = from[0];
79           to_next[0] = bi0;
80           from += 1;
81           to_next += 1;
82           n_left_from -= 1;
83           n_left_to_next -= 1;
84
85           b0 = vlib_get_buffer (vm, bi0);
86
87           /*
88            * lookup the uplink based on src EPG
89            */
90           src_epg = vnet_buffer2 (b0)->gbp.src_epg;
91
92           sw_if_index0 = gbp_epg_itf_lookup (src_epg);
93
94           if (~0 != sw_if_index0)
95             {
96               vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
97
98               next0 = GBP_FWD_NEXT_OUTPUT;
99             }
100           /*
101            * else
102            *  don't know the uplink interface for this EPG => drop
103            */
104
105           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
106             {
107               gbp_fwd_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
108               t->src_epg = src_epg;
109               t->sw_if_index = sw_if_index0;
110             }
111
112           /* verify speculative enqueue, maybe switch current next frame */
113           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
114                                            to_next, n_left_to_next,
115                                            bi0, next0);
116         }
117
118       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
119     }
120
121   return frame->n_vectors;
122 }
123
124 /* packet trace format function */
125 static u8 *
126 format_gbp_fwd_trace (u8 * s, va_list * args)
127 {
128   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
129   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
130   gbp_fwd_trace_t *t = va_arg (*args, gbp_fwd_trace_t *);
131
132   s = format (s, "src-epg:%d", t->src_epg);
133
134   return s;
135 }
136
137 /* *INDENT-OFF* */
138 VLIB_REGISTER_NODE (gbp_fwd_node) = {
139   .name = "gbp-fwd",
140   .vector_size = sizeof (u32),
141   .format_trace = format_gbp_fwd_trace,
142   .type = VLIB_NODE_TYPE_INTERNAL,
143
144   .n_errors = ARRAY_LEN(gbp_fwd_error_strings),
145   .error_strings = gbp_fwd_error_strings,
146
147   .n_next_nodes = GBP_FWD_N_NEXT,
148
149   .next_nodes = {
150     [GBP_FWD_NEXT_DROP] = "error-drop",
151     [GBP_FWD_NEXT_OUTPUT] = "l2-output",
152   },
153 };
154 /* *INDENT-ON* */
155
156 /*
157  * fd.io coding-style-patch-verification: ON
158  *
159  * Local Variables:
160  * eval: (c-set-style "gnu")
161  * End:
162  */