49f5a15fb5cc27e72f590d27789e0fcfa6fe3da1
[vpp.git] / vnet / vnet / l2tp / encap.c
1 /*
2  * encap.c : L2TPv3 tunnel encapsulation
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 #include <vppinfra/error.h>
19 #include <vppinfra/hash.h>
20 #include <vnet/vnet.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/l2tp/l2tp.h>
24
25 /* Statistics (not really errors) */
26 #define foreach_l2t_encap_error                                 \
27 _(NETWORK_TO_USER, "L2TP L2 network to user (ip6) pkts")        \
28 _(LOOKUP_FAIL_TO_L3, "L2TP L2 session lookup failed pkts")      \
29 _(ADMIN_DOWN, "L2TP tunnel is down")
30
31 static char * l2t_encap_error_strings[] = {
32 #define _(sym,string) string,
33   foreach_l2t_encap_error
34 #undef _
35 };
36
37 typedef enum {
38 #define _(sym,str) L2T_ENCAP_ERROR_##sym,
39     foreach_l2t_encap_error
40 #undef _
41     L2T_ENCAP_N_ERROR,
42 } l2t_encap_error_t;
43
44
45 typedef enum {
46     L2T_ENCAP_NEXT_DROP,
47     L2T_ENCAP_NEXT_IP6_LOOKUP,
48     L2T_ENCAP_N_NEXT,
49 } l2t_encap_next_t;
50
51 typedef struct {
52   u32 cached_session_index;
53   u32 cached_sw_if_index;
54   vnet_main_t * vnet_main;
55 } l2tp_encap_runtime_t;
56
57 vlib_node_registration_t l2t_encap_node;
58
59 #define NSTAGES 3
60
61 static inline void stage0 (vlib_main_t * vm,
62                            vlib_node_runtime_t * node,
63                            u32 buffer_index)
64 {
65     vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
66     vlib_prefetch_buffer_header (b, STORE);
67     CLIB_PREFETCH (b->data, 2*CLIB_CACHE_LINE_BYTES, STORE);
68 }
69
70 static inline void stage1 (vlib_main_t * vm,
71                            vlib_node_runtime_t * node,
72                            u32 bi)
73 {
74     l2tp_encap_runtime_t * rt = (void *) node->runtime_data;
75     vlib_buffer_t *b = vlib_get_buffer (vm, bi);
76     vnet_hw_interface_t * hi;
77
78     u32 sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
79     u32 session_index = rt->cached_session_index;
80
81     if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index))
82     {
83         hi = vnet_get_sup_hw_interface (rt->vnet_main, sw_if_index);
84         session_index = rt->cached_session_index = hi->dev_instance;
85         rt->cached_sw_if_index = sw_if_index;        
86     }
87
88     /* Remember mapping index, prefetch the mini counter */
89     vnet_buffer(b)->l2t.next_index = L2T_ENCAP_NEXT_IP6_LOOKUP;
90     vnet_buffer(b)->l2t.session_index = session_index;
91
92     /* $$$$ prefetch counter... */
93 }
94
95 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
96                               u32 bi)
97 {
98     vlib_buffer_t *b = vlib_get_buffer (vm, bi);
99     l2t_main_t *lm = &l2t_main;
100     vlib_node_t *n = vlib_get_node (vm, l2t_encap_node.index);
101     u32 node_counter_base_index = n->error_heap_index;
102     vlib_error_main_t * em = &vm->error_main;
103     l2tpv3_header_t * l2tp;
104     u32 session_index;
105     u32 counter_index;
106     l2t_session_t *s;
107     ip6_header_t *ip6;
108     u16 payload_length;
109     u32 next_index = L2T_ENCAP_NEXT_IP6_LOOKUP;
110
111     /* Other-than-output pkt? We're done... */
112     if (vnet_buffer(b)->l2t.next_index != L2T_ENCAP_NEXT_IP6_LOOKUP)
113         return vnet_buffer(b)->l2t.next_index;
114
115     /* clear so it is not interpreted as fib_index */
116     vnet_buffer(b)->sw_if_index[VLIB_TX] = (u32)~0;
117
118     em->counters[node_counter_base_index + L2T_ENCAP_ERROR_NETWORK_TO_USER] += 1;
119     
120     session_index = vnet_buffer(b)->l2t.session_index;
121
122     counter_index = 
123         session_index_to_counter_index (session_index,
124                                         SESSION_COUNTER_NETWORK_TO_USER);
125     
126     /* per-mapping byte stats include the ethernet header */
127     vlib_increment_combined_counter (&lm->counter_main,
128                                      os_get_cpu_number(),
129                                      counter_index, 
130                                      1 /* packet_increment */,
131                                      vlib_buffer_length_in_chain (vm, b));
132     
133     s = pool_elt_at_index (lm->sessions, session_index);
134
135     /* Paint on an l2tpv3 hdr */
136     vlib_buffer_advance (b, -(s->l2tp_hdr_size));
137     l2tp = vlib_buffer_get_current (b);
138
139     l2tp->session_id = s->remote_session_id;
140     l2tp->cookie = s->remote_cookie;
141     if (PREDICT_FALSE (s->l2_sublayer_present)) {
142         l2tp->l2_specific_sublayer = 0;
143     }
144
145     /* Paint on an ip6 header */
146     vlib_buffer_advance (b, -(sizeof (*ip6)));
147     ip6 = vlib_buffer_get_current (b);
148
149     if (PREDICT_FALSE(!(s->admin_up))) {
150         b->error = node->errors[L2T_ENCAP_ERROR_ADMIN_DOWN];
151         next_index = L2T_ENCAP_NEXT_DROP;
152         goto done;
153     }
154
155     ip6->ip_version_traffic_class_and_flow_label = 
156         clib_host_to_net_u32 (0x6<<28);
157
158     /* calculate ip6 payload length */
159     payload_length = vlib_buffer_length_in_chain (vm, b);
160     payload_length -= sizeof (*ip6);
161
162     ip6->payload_length = clib_host_to_net_u16 (payload_length);
163     ip6->protocol = IP_PROTOCOL_L2TP;
164     ip6->hop_limit = 0xff;
165     ip6->src_address.as_u64[0] = s->our_address.as_u64[0];
166     ip6->src_address.as_u64[1] = s->our_address.as_u64[1];
167     ip6->dst_address.as_u64[0] = s->client_address.as_u64[0];
168     ip6->dst_address.as_u64[1] = s->client_address.as_u64[1];
169
170
171  done:
172     if (PREDICT_FALSE(b->flags & VLIB_BUFFER_IS_TRACED)) {
173         l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
174         t->is_user_to_network = 0;
175         t->our_address.as_u64[0] =
176             ip6->src_address.as_u64[0];
177         t->our_address.as_u64[1] =
178             ip6->src_address.as_u64[1];
179         t->client_address.as_u64[0] =
180             ip6->dst_address.as_u64[0];
181         t->client_address.as_u64[1] =
182             ip6->dst_address.as_u64[1];
183         t->session_index = session_index;
184     }
185
186     return next_index;
187 }
188
189 #include <vnet/pipeline.h>
190
191 uword l2t_encap_node_fn (vlib_main_t * vm,
192                       vlib_node_runtime_t * node,
193                       vlib_frame_t * frame)
194 {
195     return dispatch_pipeline (vm, node, frame);
196 }
197
198
199 VLIB_REGISTER_NODE (l2t_encap_node) = {
200   .function = l2t_encap_node_fn,
201   .name = "l2tp-encap",
202   .vector_size = sizeof (u32),
203   .format_trace = format_l2t_trace,
204   .type = VLIB_NODE_TYPE_INTERNAL,
205   .runtime_data_bytes = sizeof (l2tp_encap_runtime_t),
206   
207   .n_errors = ARRAY_LEN(l2t_encap_error_strings),
208   .error_strings = l2t_encap_error_strings,
209
210   .n_next_nodes = L2T_ENCAP_N_NEXT,
211
212   //  add dispositions here
213   .next_nodes = {
214         [L2T_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
215         [L2T_ENCAP_NEXT_DROP] = "error-drop",
216   },
217 };
218
219 VLIB_NODE_FUNCTION_MULTIARCH (l2t_encap_node, l2t_encap_node_fn)
220
221 void l2tp_encap_init (vlib_main_t * vm)
222 {
223   l2tp_encap_runtime_t * rt;
224
225   rt = vlib_node_get_runtime_data (vm, l2t_encap_node.index);
226   rt->vnet_main = vnet_get_main();
227   rt->cached_sw_if_index = (u32) ~0;
228   rt->cached_session_index = (u32) ~0;
229 }