77bc88fea609cda897144d4a52dbd694e22c8ed7
[vpp.git] / src / plugins / pppoe / pppoe.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #ifndef _PPPOE_H
19 #define _PPPOE_H
20
21 #include <vnet/plugin/plugin.h>
22 #include <vppinfra/lock.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/hash.h>
25 #include <vnet/vnet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/dpo/dpo.h>
31 #include <vnet/adj/adj_types.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vlib/vlib.h>
34 #include <vppinfra/bihash_8_8.h>
35
36
37 typedef struct
38 {
39   u8 ver_type;
40   u8 code;
41   u16 session_id;
42   u16 length;
43   u16 ppp_proto;
44 } pppoe_header_t;
45
46 #define PPPOE_VER_TYPE 0x11
47 #define PPPOE_PADS 0x65
48
49 typedef struct
50 {
51   /* pppoe session_id in HOST byte order */
52   u16 session_id;
53
54   /* session client addresses */
55   ip46_address_t client_ip;
56
57   /* the index of tx interface for pppoe encaped packet */
58   u32 encap_if_index;
59
60   /** FIB indices - inner IP packet lookup here */
61   u32 decap_fib_index;
62
63   u8 local_mac[6];
64   u8 client_mac[6];
65
66   /* vnet intfc index */
67   u32 sw_if_index;
68   u32 hw_if_index;
69
70 } pppoe_session_t;
71
72 #define foreach_pppoe_input_next        \
73 _(DROP, "error-drop")                  \
74 _(IP4_INPUT, "ip4-input")              \
75 _(IP6_INPUT, "ip6-input" )             \
76 _(CP_INPUT, "pppoe-cp-dispatch" )      \
77
78 typedef enum
79 {
80 #define _(s,n) PPPOE_INPUT_NEXT_##s,
81   foreach_pppoe_input_next
82 #undef _
83     PPPOE_INPUT_N_NEXT,
84 } pppoe_input_next_t;
85
86 typedef enum
87 {
88 #define pppoe_error(n,s) PPPOE_ERROR_##n,
89 #include <pppoe/pppoe_error.def>
90 #undef pppoe_error
91   PPPOE_N_ERROR,
92 } pppoe_input_error_t;
93
94
95 #define MTU 1500
96 #define MTU_BUFFERS ((MTU + VLIB_BUFFER_DATA_SIZE - 1) / VLIB_BUFFER_DATA_SIZE)
97 #define NUM_BUFFERS_TO_ALLOC 32
98
99 /*
100  * The size of pppoe session table
101  */
102 #define PPPOE_NUM_BUCKETS (64 * 1024)
103 #define PPPOE_MEMORY_SIZE (8<<20)
104
105 /* *INDENT-OFF* */
106 /*
107  * The PPPoE key is the mac address and session ID
108  */
109 typedef struct
110 {
111   union
112   {
113     struct
114     {
115       u16 session_id;
116       u8 mac[6];
117     } fields;
118     struct
119     {
120       u32 w0;
121       u32 w1;
122     } words;
123     u64 raw;
124   };
125 } pppoe_entry_key_t;
126 /* *INDENT-ON* */
127
128 /* *INDENT-OFF* */
129 /*
130  * The PPPoE entry results
131  */
132 typedef struct
133 {
134   union
135   {
136     struct
137     {
138       u32 sw_if_index;
139
140       u32 session_index;
141
142     } fields;
143     u64 raw;
144   };
145 }  pppoe_entry_result_t;
146 /* *INDENT-ON* */
147
148 typedef struct
149 {
150   /* Vector of encap session instances, */
151   pppoe_session_t *sessions;
152
153   /* For CP:  vector of CP path */
154     BVT (clib_bihash) link_table;
155
156   /* For DP:  vector of DP path */
157     BVT (clib_bihash) session_table;
158
159   /* Free vlib hw_if_indices */
160   u32 *free_pppoe_session_hw_if_indices;
161
162   /* Mapping from sw_if_index to session index */
163   u32 *session_index_by_sw_if_index;
164
165   /* used for pppoe cp path */
166   u32 cp_if_index;
167
168   /* API message ID base */
169   u16 msg_id_base;
170
171   /* convenience */
172   vlib_main_t *vlib_main;
173   vnet_main_t *vnet_main;
174
175 } pppoe_main_t;
176
177 extern pppoe_main_t pppoe_main;
178
179 extern vlib_node_registration_t pppoe_input_node;
180 extern vlib_node_registration_t pppoe_cp_dispatch_node;
181
182 typedef struct
183 {
184   u8 is_add;
185   u8 is_ip6;
186   u16 session_id;
187   ip46_address_t client_ip;
188   u32 encap_if_index;
189   u32 decap_fib_index;
190   u8 local_mac[6];
191   u8 client_mac[6];
192 } vnet_pppoe_add_del_session_args_t;
193
194 int vnet_pppoe_add_del_session
195   (vnet_pppoe_add_del_session_args_t * a, u32 * sw_if_indexp);
196
197 typedef struct
198 {
199   u8 is_add;
200   u32 client_if_index;
201   u32 cp_if_index;
202 } vnet_pppoe_add_del_tap_args_t;
203
204 always_inline u64
205 pppoe_make_key (u8 * mac_address, u16 session_id)
206 {
207   u64 temp;
208
209   /*
210    * The mac address in memory is A:B:C:D:E:F
211    * The session_id in register is H:L
212    */
213 #if CLIB_ARCH_IS_LITTLE_ENDIAN
214   /*
215    * Create the in-register key as F:E:D:C:B:A:H:L
216    * In memory the key is L:H:A:B:C:D:E:F
217    */
218   temp = *((u64 *) (mac_address)) << 16;
219   temp = (temp & ~0xffff) | (u64) (session_id);
220 #else
221   /*
222    * Create the in-register key as H:L:A:B:C:D:E:F
223    * In memory the key is H:L:A:B:C:D:E:F
224    */
225   temp = *((u64 *) (mac_address)) >> 16;
226   temp = temp | (((u64) session_id) << 48);
227 #endif
228
229   return temp;
230 }
231
232 /**
233  * Perform learning on one packet based on the mac table lookup result.
234  * */
235 static_always_inline void
236 pppoe_learn_process (BVT (clib_bihash) * table,
237                      u32 sw_if_index0,
238                      pppoe_entry_key_t * key0,
239                      pppoe_entry_key_t * cached_key,
240                      u32 * bucket0, pppoe_entry_result_t * result0)
241 {
242   /* Check mac table lookup result */
243   if (PREDICT_TRUE (result0->fields.sw_if_index == sw_if_index0))
244     {
245       /*
246        * The entry was in the table, and the sw_if_index matched, the normal case
247        */
248       return;
249     }
250   else if (result0->fields.sw_if_index == ~0)
251     {
252       /* The entry was not in table, so add it  */
253       result0->fields.sw_if_index = sw_if_index0;
254       result0->fields.session_index = ~0;
255       cached_key->raw = ~0;     /* invalidate the cache */
256     }
257   else
258     {
259       /* The entry was in the table, but with the wrong sw_if_index mapping (mac move) */
260       result0->fields.sw_if_index = sw_if_index0;
261     }
262
263   /* Update the entry */
264   BVT (clib_bihash_kv) kv;
265   kv.key = key0->raw;
266   kv.value = result0->raw;
267   BV (clib_bihash_add_del) (table, &kv, 1 /* is_add */ );
268 }
269
270 static_always_inline void
271 pppoe_lookup_1 (BVT (clib_bihash) * table,
272                 pppoe_entry_key_t * cached_key,
273                 pppoe_entry_result_t * cached_result,
274                 u8 * mac0,
275                 u16 session_id0,
276                 pppoe_entry_key_t * key0,
277                 u32 * bucket0, pppoe_entry_result_t * result0)
278 {
279   /* set up key */
280   key0->raw = pppoe_make_key (mac0, session_id0);
281   *bucket0 = ~0;
282
283   if (key0->raw == cached_key->raw)
284     {
285       /* Hit in the one-entry cache */
286       result0->raw = cached_result->raw;
287     }
288   else
289     {
290       /* Do a regular session table lookup */
291       BVT (clib_bihash_kv) kv;
292
293       kv.key = key0->raw;
294       kv.value = ~0ULL;
295       BV (clib_bihash_search_inline) (table, &kv);
296       result0->raw = kv.value;
297
298       /* Update one-entry cache */
299       cached_key->raw = key0->raw;
300       cached_result->raw = result0->raw;
301     }
302 }
303
304 static_always_inline void
305 pppoe_update_1 (BVT (clib_bihash) * table,
306                 u8 * mac0,
307                 u16 session_id0,
308                 pppoe_entry_key_t * key0,
309                 u32 * bucket0, pppoe_entry_result_t * result0)
310 {
311   /* set up key */
312   key0->raw = pppoe_make_key (mac0, session_id0);
313   *bucket0 = ~0;
314
315   /* Update the entry */
316   BVT (clib_bihash_kv) kv;
317   kv.key = key0->raw;
318   kv.value = result0->raw;
319   BV (clib_bihash_add_del) (table, &kv, 1 /* is_add */ );
320
321 }
322 #endif /* _PPPOE_H */
323
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "gnu")
329  * End:
330  */