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