Add basic support for DS-Lite CE (VPP-1059)
[vpp.git] / src / plugins / nat / dslite_ce_decap.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 #include <nat/dslite.h>
16
17 vlib_node_registration_t dslite_ce_decap_node;
18
19 typedef enum
20 {
21   DSLITE_CE_DECAP_NEXT_IP4_LOOKUP,
22   DSLITE_IN2OUT_NEXT_IP6_ICMP,
23   DSLITE_CE_DECAP_NEXT_DROP,
24   DSLITE_CE_DECAP_N_NEXT,
25 } dslite_ce_decap_next_t;
26
27 static char *dslite_ce_decap_error_strings[] = {
28 #define _(sym,string) string,
29   foreach_dslite_error
30 #undef _
31 };
32
33 static uword
34 dslite_ce_decap_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
35                          vlib_frame_t * frame)
36 {
37   u32 n_left_from, *from, *to_next;
38   dslite_ce_decap_next_t next_index;
39
40   from = vlib_frame_vector_args (frame);
41   n_left_from = frame->n_vectors;
42   next_index = node->cached_next_index;
43
44   while (n_left_from > 0)
45     {
46       u32 n_left_to_next;
47
48       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
49
50       while (n_left_from > 0 && n_left_to_next > 0)
51         {
52           u32 bi0;
53           vlib_buffer_t *b0;
54           u32 next0 = DSLITE_CE_DECAP_NEXT_IP4_LOOKUP;
55           u8 error0 = DSLITE_ERROR_CE_DECAP;
56           ip4_header_t *ip40;
57           ip6_header_t *ip60;
58           u32 proto0;
59
60           /* speculatively enqueue b0 to the current next frame */
61           bi0 = from[0];
62           to_next[0] = bi0;
63           from += 1;
64           to_next += 1;
65           n_left_from -= 1;
66           n_left_to_next -= 1;
67
68           b0 = vlib_get_buffer (vm, bi0);
69           ip60 = vlib_buffer_get_current (b0);
70
71           if (PREDICT_FALSE (ip60->protocol != IP_PROTOCOL_IP_IN_IP))
72             {
73               if (ip60->protocol == IP_PROTOCOL_ICMP6)
74                 {
75                   next0 = DSLITE_IN2OUT_NEXT_IP6_ICMP;
76                   goto trace0;
77                 }
78               error0 = DSLITE_ERROR_BAD_IP6_PROTOCOL;
79               next0 = DSLITE_CE_DECAP_NEXT_DROP;
80               goto trace0;
81             }
82
83           ip40 = vlib_buffer_get_current (b0) + sizeof (ip6_header_t);
84           proto0 = ip_proto_to_snat_proto (ip40->protocol);
85
86           if (PREDICT_FALSE (proto0 == ~0))
87             {
88               error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL;
89               next0 = DSLITE_CE_DECAP_NEXT_DROP;
90               goto trace0;
91             }
92
93           ip40->tos =
94             (clib_net_to_host_u32
95              (ip60->ip_version_traffic_class_and_flow_label) & 0x0ff00000) >>
96             20;
97           vlib_buffer_advance (b0, sizeof (ip6_header_t));
98
99         trace0:
100           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
101                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
102             {
103               dslite_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
104               t->next_index = next0;
105             }
106
107           b0->error = node->errors[error0];
108
109           /* verify speculative enqueue, maybe switch current next frame */
110           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
111                                            n_left_to_next, bi0, next0);
112         }
113       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
114     }
115
116   return frame->n_vectors;
117 }
118
119 /* *INDENT-OFF* */
120 VLIB_REGISTER_NODE (dslite_ce_decap_node) = {
121   .function = dslite_ce_decap_node_fn,
122   .name = "dslite-ce-decap",
123   .vector_size = sizeof (u32),
124   .format_trace = format_dslite_ce_trace,
125   .type = VLIB_NODE_TYPE_INTERNAL,
126   .n_errors = ARRAY_LEN (dslite_ce_decap_error_strings),
127   .error_strings = dslite_ce_decap_error_strings,
128   .n_next_nodes = DSLITE_CE_DECAP_N_NEXT,
129   /* edit / add dispositions here */
130   .next_nodes = {
131     [DSLITE_CE_DECAP_NEXT_DROP] = "error-drop",
132     [DSLITE_CE_DECAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
133     [DSLITE_IN2OUT_NEXT_IP6_ICMP] = "ip6-icmp-input",
134   },
135 };
136 /* *INDENT-ON* */
137
138 VLIB_NODE_FUNCTION_MULTIARCH (dslite_ce_decap_node, dslite_ce_decap_node_fn);
139
140 /*
141  * fd.io coding-style-patch-verification: ON
142  *
143  * Local Variables:
144  * eval: (c-set-style "gnu")
145  * End:
146  */