sr: SRv6 uN behavior
[vpp.git] / src / plugins / srv6-un / node.c
1 /*
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:
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 <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
18 #include <srv6-un/un.h>
19
20
21 /******************************* Packet tracing *******************************/
22 typedef struct
23 {
24   u32 localsid_index;
25 } srv6_un_localsid_trace_t;
26
27
28 static u8 *
29 format_srv6_un_localsid_trace (u8 * s, va_list * args)
30 {
31   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33   srv6_un_localsid_trace_t *t = va_arg (*args, srv6_un_localsid_trace_t *);
34
35   return format (s, "SRv6-uN-localsid: localsid_index %d", t->localsid_index);
36 }
37
38 /********************************* Next nodes *********************************/
39 typedef enum
40 {
41   SRV6_UN_LOCALSID_NEXT_ERROR,
42   SRV6_UN_LOCALSID_NEXT_IP6LOOKUP,
43   SRV6_UN_LOCALSID_N_NEXT,
44 } srv6_un_localsid_next_t;
45
46 /******************************* Local SID node *******************************/
47
48 /**
49  * @brief Graph node for applying SRv6 uN.
50  */
51 static uword
52 srv6_un_localsid_fn (vlib_main_t * vm,
53                      vlib_node_runtime_t * node, vlib_frame_t * frame)
54 {
55   ip6_sr_main_t *sm = &sr_main;
56   u32 n_left_from, next_index, *from, *to_next;
57
58   from = vlib_frame_vector_args (frame);
59   n_left_from = frame->n_vectors;
60   next_index = node->cached_next_index;
61
62   u32 thread_index = vm->thread_index;
63
64   while (n_left_from > 0)
65     {
66       u32 n_left_to_next;
67       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
68
69       /* Quad - Loop */
70       while (n_left_from >= 8 && n_left_to_next >= 4)
71         {
72           u32 bi0, bi1, bi2, bi3;
73           vlib_buffer_t *b0, *b1, *b2, *b3;
74           ip6_header_t *ip0, *ip1, *ip2, *ip3;
75           u32 next0, next1, next2, next3;
76           next0 = next1 = next2 = next3 = SRV6_UN_LOCALSID_NEXT_IP6LOOKUP;
77           ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
78
79           /* Prefetch next iteration. */
80           {
81             vlib_buffer_t *p4, *p5, *p6, *p7;
82
83             p4 = vlib_get_buffer (vm, from[4]);
84             p5 = vlib_get_buffer (vm, from[5]);
85             p6 = vlib_get_buffer (vm, from[6]);
86             p7 = vlib_get_buffer (vm, from[7]);
87
88             /* Prefetch the buffer header and packet for the N+4 loop iteration */
89             vlib_prefetch_buffer_header (p4, LOAD);
90             vlib_prefetch_buffer_header (p5, LOAD);
91             vlib_prefetch_buffer_header (p6, LOAD);
92             vlib_prefetch_buffer_header (p7, LOAD);
93
94             CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
95             CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
96             CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
97             CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
98           }
99
100           to_next[0] = bi0 = from[0];
101           to_next[1] = bi1 = from[1];
102           to_next[2] = bi2 = from[2];
103           to_next[3] = bi3 = from[3];
104           from += 4;
105           to_next += 4;
106           n_left_from -= 4;
107           n_left_to_next -= 4;
108
109           b0 = vlib_get_buffer (vm, bi0);
110           b1 = vlib_get_buffer (vm, bi1);
111           b2 = vlib_get_buffer (vm, bi2);
112           b3 = vlib_get_buffer (vm, bi3);
113
114           ls0 =
115             pool_elt_at_index (sm->localsids,
116                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
117           ls1 =
118             pool_elt_at_index (sm->localsids,
119                                vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
120           ls2 =
121             pool_elt_at_index (sm->localsids,
122                                vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
123           ls3 =
124             pool_elt_at_index (sm->localsids,
125                                vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
126
127           ip0 = vlib_buffer_get_current (b0);
128           ip1 = vlib_buffer_get_current (b1);
129           ip2 = vlib_buffer_get_current (b2);
130           ip3 = vlib_buffer_get_current (b3);
131
132           /* Set Destination Address to Last Segment (index 0) */
133           ip0->dst_address.as_u16[2] = ip0->dst_address.as_u16[3];
134           ip0->dst_address.as_u16[3] = ip0->dst_address.as_u16[4];
135           ip0->dst_address.as_u16[4] = ip0->dst_address.as_u16[5];
136           ip0->dst_address.as_u16[5] = ip0->dst_address.as_u16[6];
137           ip0->dst_address.as_u16[6] = ip0->dst_address.as_u16[7];
138           ip0->dst_address.as_u16[7] = 0x0000;
139
140           ip1->dst_address.as_u16[2] = ip1->dst_address.as_u16[3];
141           ip1->dst_address.as_u16[3] = ip1->dst_address.as_u16[4];
142           ip1->dst_address.as_u16[4] = ip1->dst_address.as_u16[5];
143           ip1->dst_address.as_u16[5] = ip1->dst_address.as_u16[6];
144           ip1->dst_address.as_u16[6] = ip1->dst_address.as_u16[7];
145           ip1->dst_address.as_u16[7] = 0x0000;
146
147           ip2->dst_address.as_u16[2] = ip2->dst_address.as_u16[3];
148           ip2->dst_address.as_u16[3] = ip2->dst_address.as_u16[4];
149           ip2->dst_address.as_u16[4] = ip2->dst_address.as_u16[5];
150           ip2->dst_address.as_u16[5] = ip2->dst_address.as_u16[6];
151           ip2->dst_address.as_u16[6] = ip2->dst_address.as_u16[7];
152           ip2->dst_address.as_u16[7] = 0x0000;
153
154           ip3->dst_address.as_u16[2] = ip3->dst_address.as_u16[3];
155           ip3->dst_address.as_u16[3] = ip3->dst_address.as_u16[4];
156           ip3->dst_address.as_u16[4] = ip3->dst_address.as_u16[5];
157           ip3->dst_address.as_u16[5] = ip3->dst_address.as_u16[6];
158           ip3->dst_address.as_u16[6] = ip3->dst_address.as_u16[7];
159           ip3->dst_address.as_u16[7] = 0x0000;
160
161           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
162             {
163               srv6_un_localsid_trace_t *tr =
164                 vlib_add_trace (vm, node, b0, sizeof (*tr));
165               tr->localsid_index = ls0 - sm->localsids;
166             }
167
168           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
169             {
170               srv6_un_localsid_trace_t *tr =
171                 vlib_add_trace (vm, node, b1, sizeof (*tr));
172               tr->localsid_index = ls1 - sm->localsids;
173             }
174
175           if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
176             {
177               srv6_un_localsid_trace_t *tr =
178                 vlib_add_trace (vm, node, b2, sizeof (*tr));
179               tr->localsid_index = ls2 - sm->localsids;
180             }
181
182           if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
183             {
184               srv6_un_localsid_trace_t *tr =
185                 vlib_add_trace (vm, node, b3, sizeof (*tr));
186               tr->localsid_index = ls3 - sm->localsids;
187             }
188
189           vlib_increment_combined_counter
190             (((next0 ==
191                SRV6_UN_LOCALSID_NEXT_ERROR) ?
192               &(sm->sr_ls_invalid_counters) :
193               &(sm->sr_ls_valid_counters)),
194              thread_index, ls0 - sm->localsids,
195              1, vlib_buffer_length_in_chain (vm, b0));
196
197           vlib_increment_combined_counter
198             (((next1 ==
199                SRV6_UN_LOCALSID_NEXT_ERROR) ?
200               &(sm->sr_ls_invalid_counters) :
201               &(sm->sr_ls_valid_counters)),
202              thread_index, ls1 - sm->localsids,
203              1, vlib_buffer_length_in_chain (vm, b1));
204
205           vlib_increment_combined_counter
206             (((next2 ==
207                SRV6_UN_LOCALSID_NEXT_ERROR) ?
208               &(sm->sr_ls_invalid_counters) :
209               &(sm->sr_ls_valid_counters)),
210              thread_index, ls2 - sm->localsids,
211              1, vlib_buffer_length_in_chain (vm, b2));
212
213           vlib_increment_combined_counter
214             (((next3 ==
215                SRV6_UN_LOCALSID_NEXT_ERROR) ?
216               &(sm->sr_ls_invalid_counters) :
217               &(sm->sr_ls_valid_counters)),
218              thread_index, ls3 - sm->localsids,
219              1, vlib_buffer_length_in_chain (vm, b3));
220
221           vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
222                                            n_left_to_next, bi0, bi1, bi2, bi3,
223                                            next0, next1, next2, next3);
224         }
225
226       while (n_left_from > 0 && n_left_to_next > 0)
227         {
228           u32 bi0;
229           vlib_buffer_t *b0;
230           ip6_header_t *ip0 = 0;
231           u32 next0 = SRV6_UN_LOCALSID_NEXT_IP6LOOKUP;
232           ip6_sr_localsid_t *ls0;
233           bi0 = from[0];
234           to_next[0] = bi0;
235           from += 1;
236           to_next += 1;
237           n_left_from -= 1;
238           n_left_to_next -= 1;
239
240           b0 = vlib_get_buffer (vm, bi0);
241           ip0 = vlib_buffer_get_current (b0);
242
243           /* Lookup the SR End behavior based on IP DA (adj) */
244           ls0 =
245             pool_elt_at_index (sm->localsids,
246                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
247
248           /* Set Destination Address to Last Segment (index 0) */
249           ip0->dst_address.as_u16[2] = ip0->dst_address.as_u16[3];
250           ip0->dst_address.as_u16[3] = ip0->dst_address.as_u16[4];
251           ip0->dst_address.as_u16[4] = ip0->dst_address.as_u16[5];
252           ip0->dst_address.as_u16[5] = ip0->dst_address.as_u16[6];
253           ip0->dst_address.as_u16[6] = ip0->dst_address.as_u16[7];
254           ip0->dst_address.as_u16[7] = 0x0000;
255
256           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
257             {
258               srv6_un_localsid_trace_t *tr =
259                 vlib_add_trace (vm, node, b0, sizeof (*tr));
260               tr->localsid_index = ls0 - sm->localsids;
261             }
262
263           /* This increments the SRv6 per LocalSID counters. */
264           vlib_increment_combined_counter (((next0 ==
265                                              SRV6_UN_LOCALSID_NEXT_ERROR) ?
266                                             &(sm->sr_ls_invalid_counters) :
267                                             &(sm->sr_ls_valid_counters)),
268                                            thread_index, ls0 - sm->localsids,
269                                            1, vlib_buffer_length_in_chain (vm,
270                                                                            b0));
271
272           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
273                                            n_left_to_next, bi0, next0);
274
275         }
276
277       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
278     }
279
280   return frame->n_vectors;
281 }
282
283 /* *INDENT-OFF* */
284 VLIB_REGISTER_NODE (srv6_un_localsid_node) = {
285   .function = srv6_un_localsid_fn,
286   .name = "srv6-un-localsid",
287   .vector_size = sizeof (u32),
288   .format_trace = format_srv6_un_localsid_trace,
289   .type = VLIB_NODE_TYPE_INTERNAL,
290   .n_next_nodes = SRV6_UN_LOCALSID_N_NEXT,
291   .next_nodes = {
292     [SRV6_UN_LOCALSID_NEXT_IP6LOOKUP] = "ip6-lookup",
293     [SRV6_UN_LOCALSID_NEXT_ERROR] = "error-drop",
294   },
295 };
296 /* *INDENT-ON* */
297
298 /*
299 * fd.io coding-style-patch-verification: ON
300 *
301 * Local Variables:
302 * eval: (c-set-style "gnu")
303 * End:
304 */