Add PPPoE Plugin
[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   /* Rewrite string */
52   u8 *rewrite;
53
54   /* pppoe session_id in HOST byte order */
55   u16 session_id;
56
57   /* session client addresses */
58   ip46_address_t client_ip;
59
60   /* the index of tx interface for pppoe encaped packet */
61   u32 encap_if_index;
62
63   /** FIB indices - inner IP packet lookup here */
64   u32 decap_fib_index;
65
66   u8 local_mac[6];
67   u8 client_mac[6];
68
69   /* vnet intfc index */
70   u32 sw_if_index;
71   u32 hw_if_index;
72
73 } pppoe_session_t;
74
75 #define foreach_pppoe_input_next        \
76 _(DROP, "error-drop")                  \
77 _(IP4_INPUT, "ip4-input")              \
78 _(IP6_INPUT, "ip6-input" )             \
79 _(CP_INPUT, "pppoe-tap-dispatch" )     \
80
81 typedef enum
82 {
83 #define _(s,n) PPPOE_INPUT_NEXT_##s,
84   foreach_pppoe_input_next
85 #undef _
86     PPPOE_INPUT_N_NEXT,
87 } pppoe_input_next_t;
88
89 typedef enum
90 {
91 #define pppoe_error(n,s) PPPOE_ERROR_##n,
92 #include <pppoe/pppoe_error.def>
93 #undef pppoe_error
94   PPPOE_N_ERROR,
95 } pppoe_input_error_t;
96
97
98 #define MTU 1500
99 #define MTU_BUFFERS ((MTU + VLIB_BUFFER_DATA_SIZE - 1) / VLIB_BUFFER_DATA_SIZE)
100 #define NUM_BUFFERS_TO_ALLOC 32
101
102 /*
103  * The size of pppoe session table
104  */
105 #define PPPOE_NUM_BUCKETS (128 * 1024)
106 #define PPPOE_MEMORY_SIZE (16<<20)
107
108 /* *INDENT-OFF* */
109 /*
110  * The PPPoE key is the mac address and session ID
111  */
112 typedef struct
113 {
114   union
115   {
116     struct
117     {
118       u16 session_id;
119       u8 mac[6];
120     } fields;
121     struct
122     {
123       u32 w0;
124       u32 w1;
125     } words;
126     u64 raw;
127   };
128 } pppoe_entry_key_t;
129 /* *INDENT-ON* */
130
131 /* *INDENT-OFF* */
132 /*
133  * The PPPoE entry results
134  */
135 typedef struct
136 {
137   union
138   {
139     struct
140     {
141       u32 sw_if_index;
142
143       u32 session_index;
144
145     } fields;
146     u64 raw;
147   };
148 }  pppoe_entry_result_t;
149 /* *INDENT-ON* */
150
151 typedef struct
152 {
153   /* For DP: vector of encap session instances, */
154   pppoe_session_t *sessions;
155
156   /* For CP:  vector of CP 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 tap_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_encap_node;
181 extern vlib_node_registration_t pppoe_tap_dispatch_node;
182
183 u8 *format_pppoe_encap_trace (u8 * s, va_list * args);
184
185 typedef struct
186 {
187   u8 is_add;
188   u8 is_ip6;
189   u16 session_id;
190   ip46_address_t client_ip;
191   u32 encap_if_index;
192   u32 decap_fib_index;
193   u8 local_mac[6];
194   u8 client_mac[6];
195 } vnet_pppoe_add_del_session_args_t;
196
197 int vnet_pppoe_add_del_session
198   (vnet_pppoe_add_del_session_args_t * a, u32 * sw_if_indexp);
199
200 typedef struct
201 {
202   u8 is_add;
203   u32 client_if_index;
204   u32 tap_if_index;
205 } vnet_pppoe_add_del_tap_args_t;
206
207 always_inline u64
208 pppoe_make_key (u8 * mac_address, u16 session_id)
209 {
210   u64 temp;
211
212   /*
213    * The mac address in memory is A:B:C:D:E:F
214    * The session_id in register is H:L
215    */
216 #if CLIB_ARCH_IS_LITTLE_ENDIAN
217   /*
218    * Create the in-register key as F:E:D:C:B:A:H:L
219    * In memory the key is L:H:A:B:C:D:E:F
220    */
221   temp = *((u64 *) (mac_address)) << 16;
222   temp = (temp & ~0xffff) | (u64) (session_id);
223 #else
224   /*
225    * Create the in-register key as H:L:A:B:C:D:E:F
226    * In memory the key is H:L:A:B:C:D:E:F
227    */
228   temp = *((u64 *) (mac_address)) >> 16;
229   temp = temp | (((u64) session_id) << 48);
230 #endif
231
232   return temp;
233 }
234
235 static_always_inline void
236 pppoe_lookup_1 (BVT (clib_bihash) * session_table,
237                 pppoe_entry_key_t * cached_key,
238                 pppoe_entry_result_t * cached_result,
239                 u8 * mac0,
240                 u16 session_id0,
241                 pppoe_entry_key_t * key0,
242                 u32 * bucket0, pppoe_entry_result_t * result0)
243 {
244   /* set up key */
245   key0->raw = pppoe_make_key (mac0, session_id0);
246   *bucket0 = ~0;
247
248   if (key0->raw == cached_key->raw)
249     {
250       /* Hit in the one-entry cache */
251       result0->raw = cached_result->raw;
252     }
253   else
254     {
255       /* Do a regular session table lookup */
256       BVT (clib_bihash_kv) kv;
257
258       kv.key = key0->raw;
259       kv.value = ~0ULL;
260       BV (clib_bihash_search_inline) (session_table, &kv);
261       result0->raw = kv.value;
262
263       /* Update one-entry cache */
264       cached_key->raw = key0->raw;
265       cached_result->raw = result0->raw;
266     }
267 }
268
269 static_always_inline void
270 pppoe_update_1 (BVT (clib_bihash) * session_table,
271                 u8 * mac0,
272                 u16 session_id0,
273                 pppoe_entry_key_t * key0,
274                 u32 * bucket0, pppoe_entry_result_t * result0)
275 {
276   /* set up key */
277   key0->raw = pppoe_make_key (mac0, session_id0);
278   *bucket0 = ~0;
279
280   /* Update the entry */
281   BVT (clib_bihash_kv) kv;
282   kv.key = key0->raw;
283   kv.value = result0->raw;
284   BV (clib_bihash_add_del) (session_table, &kv, 1 /* is_add */ );
285
286 }
287 #endif /* _PPPOE_H */
288
289 /*
290  * fd.io coding-style-patch-verification: ON
291  *
292  * Local Variables:
293  * eval: (c-set-style "gnu")
294  * End:
295  */