FD.io VPP  v16.06
Vector Packet Processing
l2t_l2.c
Go to the documentation of this file.
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 <vnet/vnet.h>
16 #include <vnet/ip/ip.h>
17 #include <vnet/ethernet/ethernet.h>
18 
19 #if DPDK == 0
20 #include <vnet/devices/pci/ixgev.h>
21 #include <vnet/devices/pci/ixge.h>
22 #include <vnet/devices/pci/ige.h>
23 #else
24 #include <vnet/devices/dpdk/dpdk.h>
25 #endif
26 
27 #include <vppinfra/error.h>
28 #include <vppinfra/hash.h>
29 #include <app/l2t.h>
30 
32 
33 /* Statistics (not really errors) */
34 #define foreach_l2t_l2_error \
35 _(NETWORK_TO_USER, "L2 network to user (ip6) pkts")
36 
37 static char * l2t_l2_error_strings[] = {
38 #define _(sym,string) string,
40 #undef _
41 };
42 
43 typedef enum {
44 #define _(sym,str) L2T_L2_ERROR_##sym,
46 #undef _
49 
50 /*
51  * Packets go to ethernet-input when they don't match a mapping
52  */
53 typedef enum {
59 
61 
62 #define NSTAGES 3
63 
64 static inline void stage0 (vlib_main_t * vm,
65  vlib_node_runtime_t * node,
66  u32 buffer_index)
67 {
68  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
69  vlib_prefetch_buffer_header (b, STORE);
71 }
72 
73 static inline void stage1 (vlib_main_t * vm,
74  vlib_node_runtime_t * node,
75  u32 bi)
76 {
77  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
78  l2t_main_t *lm = &l2t_main;
79  ethernet_header_t * eh;
81  u32 session_index;
82  uword *p;
83  uword vlan_and_sw_if_index_key;
84 
85  /* just in case, needed to test with the tun/tap device */
87 
88  eh = vlib_buffer_get_current (b);
89 
90  /* Not a VLAN pkt? send to ethernet-input... */
91  if (PREDICT_FALSE(eh->type != clib_host_to_net_u16 (0x8100))) {
92  vnet_buffer(b)->l2t.next_index = L2T_L2_NEXT_ETHERNET_INPUT;
93  return;
94  }
95  vh = (ethernet_vlan_header_t *)(eh+1);
96 
97  /* look up session */
98  vlan_and_sw_if_index_key = ((uword)(vh->priority_cfi_and_id)<<32)
99  | vnet_buffer(b)->sw_if_index[VLIB_RX];
100 
101  p = hash_get (lm->session_by_vlan_and_rx_sw_if_index,
102  vlan_and_sw_if_index_key);
103 
104  if (PREDICT_FALSE(p == 0)) {
105  /* $$$ drop here if not for our MAC? */
106  vnet_buffer(b)->l2t.next_index = L2T_L2_NEXT_ETHERNET_INPUT;
107  return;
108  } else {
109  session_index = p[0];
110  }
111 
112  /* Remember mapping index, prefetch the mini counter */
113  vnet_buffer(b)->l2t.next_index = L2T_L2_NEXT_IP6_LOOKUP;
114  vnet_buffer(b)->l2t.session_index = session_index;
115 
116  /* Each mapping has 2 x (pkt, byte) counters, hence the shift */
117  CLIB_PREFETCH(lm->counter_main.mini + (p[0]<<1), CLIB_CACHE_LINE_BYTES,
118  STORE);
119 }
120 
121 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
122  u32 bi)
123 {
124  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
125  l2t_main_t *lm = &l2t_main;
127  vlib_node_t *n = vlib_get_node (vm, l2t_l2_node.index);
128  u32 node_counter_base_index = n->error_heap_index;
129  vlib_error_main_t * em = &vm->error_main;
130  l2tpv3_header_t * l2t; /* l2 header */
131  ethernet_vlan_header_t * vh; /* 802.1q vlan header */
133  l2t_session_t *s;
134  ip6_header_t *ip6;
135  u16 payload_ethertype;
136  u8 dst_mac_address[6];
137  u8 src_mac_address[6];
138  u16 payload_length;
139  i32 backup;
140 
141  /* Other-than-output pkt? We're done... */
142  if (vnet_buffer(b)->l2t.next_index != L2T_L2_NEXT_IP6_LOOKUP)
143  return vnet_buffer(b)->l2t.next_index;
144 
145  vh = (ethernet_vlan_header_t *)(eh+1);
146 
147  em->counters[node_counter_base_index + L2T_L2_ERROR_NETWORK_TO_USER] += 1;
148 
149  counter_index =
150  session_index_to_counter_index (vnet_buffer(b)->l2t.session_index,
152 
153  /* per-mapping byte stats include the ethernet header */
154  vlib_increment_combined_counter (&lm->counter_main, counter_index,
155  1 /* packet_increment */,
157  sizeof (ethernet_header_t));
158 
159  s = pool_elt_at_index (lm->sessions, vnet_buffer(b)->l2t.session_index);
160 
161  /* Save src/dst MAC addresses */
162 #define _(i) dst_mac_address[i] = eh->dst_address[i];
163  _(0) _(1) _(2) _(3) _(4) _(5);
164 #undef _
165 #define _(i) src_mac_address[i] = eh->src_address[i];
166  _(0) _(1) _(2) _(3) _(4) _(5);
167 #undef _
168 
169  payload_ethertype = vh->type;
170 
171  /* Splice out the 802.1q vlan tag */
172  vlib_buffer_advance (b, 4);
173  eh = vlib_buffer_get_current (b);
174 
175  /* restore src/dst MAC addresses */
176 #define _(i) eh->dst_address[i] = dst_mac_address[i];
177  _(0) _(1) _(2) _(3) _(4) _(5);
178 #undef _
179 #define _(i) eh->src_address[i] = src_mac_address[i];
180  _(0) _(1) _(2) _(3) _(4) _(5);
181 #undef _
182  eh->type = payload_ethertype;
183 
184  /* Paint on an l2tpv3 hdr */
185  backup = sizeof(*l2t);
186 #if 0
187  /* back up 4 bytes less if no l2 sublayer */
188  backup -= s->l2_sublayer_present ? 0 : 4;
189 #endif
190 
191  vlib_buffer_advance (b, -backup);
192  l2t = vlib_buffer_get_current (b);
193 
194  l2t->session_id = s->remote_session_id;
195  l2t->cookie = s->remote_cookie;
196 
197 #if 0
198  if (s->l2_sublayer_present)
199  l2t->l2_specific_sublayer = 0;
200 #endif
201 
202  /* Paint on an ip6 header */
203  vlib_buffer_advance (b, -(sizeof (*ip6)));
204  ip6 = vlib_buffer_get_current (b);
205 
207  clib_host_to_net_u32 (0x6<<28);
208 
209  /* calculate ip6 payload length */
210  payload_length = vlib_buffer_length_in_chain (vm, b);
211  payload_length -= sizeof (*ip6);
212 
213  ip6->payload_length = clib_host_to_net_u16 (payload_length);
214  ip6->protocol = 0x73; /* l2tpv3 */
215  ip6->hop_limit = 0xff;
216  ip6->src_address.as_u64[0] = s->our_address.as_u64[0];
217  ip6->src_address.as_u64[1] = s->our_address.as_u64[1];
218  ip6->dst_address.as_u64[0] = s->client_address.as_u64[0];
219  ip6->dst_address.as_u64[1] = s->client_address.as_u64[1];
220 
221  return L2T_L2_NEXT_IP6_LOOKUP;
222 }
223 
224 #include <vnet/pipeline.h>
225 
227  vlib_node_runtime_t * node,
228  vlib_frame_t * frame)
229 {
230  return dispatch_pipeline (vm, node, frame);
231 }
232 
234  .function = l2t_l2_node_fn,
235  .name = "l2t-l2-input",
236  .vector_size = sizeof (u32),
237  .format_trace = format_l2t_trace,
239 
240  .n_errors = ARRAY_LEN(l2t_l2_error_strings),
241  .error_strings = l2t_l2_error_strings,
242 
243  .n_next_nodes = L2T_L2_N_NEXT,
244 
245  /* edit / add dispositions here */
246  .next_nodes = {
247  [L2T_L2_NEXT_IP6_LOOKUP] = "ip6-lookup",
248  [L2T_L2_NEXT_ETHERNET_INPUT] = "ethernet-input",
249  [L2T_L2_NEXT_DROP] = "error-drop",
250  },
251 };
252 
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
Definition: l2t_l2.c:121
u32 error_heap_index
Definition: node.h:244
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u64 as_u64[2]
Definition: ip6_packet.h:50
ip6_address_t our_address
Definition: l2tp.h:27
struct _vlib_node_registration vlib_node_registration_t
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
Definition: l2t_l2.c:73
ip6_address_t src_address
Definition: ip6_packet.h:293
u64 remote_cookie
Definition: l2tp.h:34
static u32 session_index_to_counter_index(u32 session_index, u32 counter_id)
Definition: l2tp.h:96
l2t_l2_next_t
Definition: l2t_l2.c:53
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
static char * l2t_l2_error_strings[]
Definition: l2t_l2.c:37
ip6_address_t client_address
Definition: l2tp.h:28
static void stage0(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
Definition: l2t_l2.c:64
always_inline void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Definition: counter.h:210
int i32
Definition: types.h:81
always_inline 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:112
vlib_node_registration_t l2t_l2_node
(constructor) VLIB_REGISTER_NODE (l2t_l2_node)
Definition: l2t_l2.c:60
#define foreach_l2t_l2_error
Definition: l2t_l2.c:34
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
vlib_error_main_t error_main
Definition: main.h:124
u8 * format_l2t_trace(u8 *s, va_list *args)
Definition: l2tp.c:29
#define PREDICT_FALSE(x)
Definition: clib.h:97
always_inline void vlib_buffer_reset(vlib_buffer_t *b)
Reset current header & length to state they were in when packet was received.
Definition: buffer.h:211
u64 * counters
Definition: error.h:73
l2t_l2_error_t
Definition: l2t_l2.c:43
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
u32 remote_session_id
Definition: l2tp.h:36
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
vlib_combined_counter_main_t counter_main
Definition: l2tp.h:65
u64 uword
Definition: types.h:112
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:280
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:284
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
unsigned char u8
Definition: types.h:56
static uword l2t_l2_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: l2t_l2.c:226
l2t_session_t * sessions
Definition: l2tp.h:55
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
u8 l2_sublayer_present
Definition: l2tp.h:43
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
always_inline u32 counter_index(vlib_main_t *vm, vlib_error_t e)
l2t_main_t l2t_main
Definition: l2t_l2.c:31
Definition: defs.h:45
ip6_address_t dst_address
Definition: ip6_packet.h:293