FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
encap.c
Go to the documentation of this file.
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,
34 #undef _
35 };
36 
37 typedef enum
38 {
39 #define _(sym,str) L2T_ENCAP_ERROR_##sym,
41 #undef _
44 
45 
46 typedef enum
47 {
52 
53 typedef struct
54 {
59 
61 
62 #define NSTAGES 3
63 
64 static inline void
65 stage0 (vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
66 {
67  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
68  vlib_prefetch_buffer_header (b, STORE);
69  CLIB_PREFETCH (b->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
70 }
71 
72 static inline void
74 {
75  l2tp_encap_runtime_t *rt = (void *) node->runtime_data;
76  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
78 
79  u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
80  u32 session_index = rt->cached_session_index;
81 
82  if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index))
83  {
84  hi = vnet_get_sup_hw_interface (rt->vnet_main, sw_if_index);
85  session_index = rt->cached_session_index = hi->dev_instance;
86  rt->cached_sw_if_index = sw_if_index;
87  }
88 
89  /* Remember mapping index, prefetch the mini counter */
90  vnet_buffer (b)->l2t.next_index = L2T_ENCAP_NEXT_IP6_LOOKUP;
91  vnet_buffer (b)->l2t.session_index = session_index;
92 
93  /* $$$$ prefetch counter... */
94 }
95 
96 static inline u32
98 {
99  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
100  l2t_main_t *lm = &l2t_main;
101  vlib_node_t *n = vlib_get_node (vm, l2t_encap_node.index);
102  u32 node_counter_base_index = n->error_heap_index;
103  vlib_error_main_t *em = &vm->error_main;
104  l2tpv3_header_t *l2tp;
105  u32 session_index;
107  l2t_session_t *s;
108  ip6_header_t *ip6;
109  u16 payload_length;
110  u32 next_index = L2T_ENCAP_NEXT_IP6_LOOKUP;
111 
112  /* Other-than-output pkt? We're done... */
113  if (vnet_buffer (b)->l2t.next_index != L2T_ENCAP_NEXT_IP6_LOOKUP)
114  return vnet_buffer (b)->l2t.next_index;
115 
116  em->counters[node_counter_base_index + L2T_ENCAP_ERROR_NETWORK_TO_USER] +=
117  1;
118 
119  session_index = vnet_buffer (b)->l2t.session_index;
120 
121  counter_index =
122  session_index_to_counter_index (session_index,
124 
125  /* per-mapping byte stats include the ethernet header */
128  counter_index, 1 /* packet_increment */ ,
130 
131  s = pool_elt_at_index (lm->sessions, session_index);
132 
133  vnet_buffer (b)->sw_if_index[VLIB_TX] = s->encap_fib_index;
134 
135  /* Paint on an l2tpv3 hdr */
137  l2tp = vlib_buffer_get_current (b);
138 
139  l2tp->session_id = s->remote_session_id;
140  l2tp->cookie = s->remote_cookie;
142  {
143  l2tp->l2_specific_sublayer = 0;
144  }
145 
146  /* Paint on an ip6 header */
147  vlib_buffer_advance (b, -(sizeof (*ip6)));
148  ip6 = vlib_buffer_get_current (b);
149 
150  if (PREDICT_FALSE (!(s->admin_up)))
151  {
152  b->error = node->errors[L2T_ENCAP_ERROR_ADMIN_DOWN];
153  next_index = L2T_ENCAP_NEXT_DROP;
154  goto done;
155  }
156 
158  clib_host_to_net_u32 (0x6 << 28);
159 
160  /* calculate ip6 payload length */
161  payload_length = vlib_buffer_length_in_chain (vm, b);
162  payload_length -= sizeof (*ip6);
163 
164  ip6->payload_length = clib_host_to_net_u16 (payload_length);
165  ip6->protocol = IP_PROTOCOL_L2TP;
166  ip6->hop_limit = 0xff;
167  ip6->src_address.as_u64[0] = s->our_address.as_u64[0];
168  ip6->src_address.as_u64[1] = s->our_address.as_u64[1];
169  ip6->dst_address.as_u64[0] = s->client_address.as_u64[0];
170  ip6->dst_address.as_u64[1] = s->client_address.as_u64[1];
171 
172 
173 done:
175  {
176  l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
177  t->is_user_to_network = 0;
178  t->our_address.as_u64[0] = ip6->src_address.as_u64[0];
179  t->our_address.as_u64[1] = ip6->src_address.as_u64[1];
180  t->client_address.as_u64[0] = ip6->dst_address.as_u64[0];
181  t->client_address.as_u64[1] = ip6->dst_address.as_u64[1];
182  t->session_index = session_index;
183  }
184 
185  return next_index;
186 }
187 
188 #include <vnet/pipeline.h>
189 
190 uword
192  vlib_node_runtime_t * node, vlib_frame_t * frame)
193 {
194  return dispatch_pipeline (vm, node, frame);
195 }
196 
197 
198 /* *INDENT-OFF* */
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 /* *INDENT-ON* */
219 
221 void
223 {
225 
227  rt->vnet_main = vnet_get_main ();
228  rt->cached_sw_if_index = (u32) ~ 0;
229  rt->cached_session_index = (u32) ~ 0;
230 }
231 
232 /*
233  * fd.io coding-style-patch-verification: ON
234  *
235  * Local Variables:
236  * eval: (c-set-style "gnu")
237  * End:
238  */
int is_user_to_network
Definition: l2tp.h:88
vmrglw vmrglh hi
u32 error_heap_index
Definition: node.h:278
ip6_address_t client_address
Definition: l2tp.h:91
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:211
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:464
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
u64 as_u64[2]
Definition: ip6_packet.h:51
ip6_address_t our_address
Definition: l2tp.h:28
vlib_node_registration_t l2t_encap_node
(constructor) VLIB_REGISTER_NODE (l2t_encap_node)
Definition: encap.c:60
u32 encap_fib_index
Definition: l2tp.h:42
struct _vlib_node_registration vlib_node_registration_t
VLIB_NODE_FUNCTION_MULTIARCH(l2t_encap_node, l2t_encap_node_fn)
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:107
u32 cached_session_index
Definition: encap.c:55
ip6_address_t src_address
Definition: ip6_packet.h:341
u64 remote_cookie
Definition: l2tp.h:33
static u32 session_index_to_counter_index(u32 session_index, u32 counter_id)
Definition: l2tp.h:106
ip6_address_t client_address
Definition: l2tp.h:29
l2t_encap_next_t
Definition: encap.c:46
l2t_main_t l2t_main
Definition: l2tp.c:26
static u32 counter_index(vlib_main_t *vm, vlib_error_t e)
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:171
static void stage0(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
Definition: encap.c:65
void l2tp_encap_init(vlib_main_t *vm)
Definition: encap.c:222
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
l2t_encap_error_t
Definition: encap.c:37
vlib_error_main_t error_main
Definition: main.h:138
u8 * format_l2t_trace(u8 *s, va_list *args)
Definition: l2tp.c:30
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:195
static void * vlib_node_get_runtime_data(vlib_main_t *vm, u32 node_index)
Get node runtime private data by node index.
Definition: node_funcs.h:110
#define PREDICT_FALSE(x)
Definition: clib.h:105
vnet_main_t * vnet_main
Definition: encap.c:57
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:113
ip6_address_t our_address
Definition: l2tp.h:90
u64 * counters
Definition: error.h:78
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
u32 cached_sw_if_index
Definition: encap.c:56
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
vlib_main_t * vm
Definition: buffer.c:283
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
Definition: encap.c:97
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
#define ARRAY_LEN(x)
Definition: clib.h:59
uword l2t_encap_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: encap.c:191
u32 remote_session_id
Definition: l2tp.h:35
unsigned int u32
Definition: types.h:88
#define foreach_l2t_encap_error
Definition: encap.c:26
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:208
vlib_combined_counter_main_t counter_main
Definition: l2tp.h:71
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:328
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:332
u8 l2tp_hdr_size
Definition: l2tp.h:44
l2t_session_t * sessions
Definition: l2tp.h:61
static char * l2t_encap_error_strings[]
Definition: encap.c:31
#define vnet_buffer(b)
Definition: buffer.h:326
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:159
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
u8 admin_up
Definition: l2tp.h:48
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
Definition: encap.c:73
u8 l2_sublayer_present
Definition: l2tp.h:45
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:75
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
u32 session_index
Definition: l2tp.h:89
ip6_address_t dst_address
Definition: ip6_packet.h:341