session: guard session lookup table allocs
[vpp.git] / src / vnet / session / session_lookup.c
1 /*
2  * Copyright (c) 2017-2019 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
16 /** Generate typed init functions for multiple hash table styles... */
17 #include <vppinfra/bihash_16_8.h>
18 #include <vppinfra/bihash_template.h>
19
20 #include <vppinfra/bihash_template.c>
21
22 #undef __included_bihash_template_h__
23
24 #include <vppinfra/bihash_48_8.h>
25 #include <vppinfra/bihash_template.h>
26
27 #include <vppinfra/bihash_template.c>
28 #include <vnet/session/session_lookup.h>
29 #include <vnet/session/session.h>
30 #include <vnet/session/application.h>
31
32 static session_lookup_main_t sl_main;
33
34 /**
35  * Network namespace index (i.e., fib index) to session lookup table. We
36  * should have one per network protocol type but for now we only support IP4/6
37  */
38 static u32 *fib_index_to_table_index[2];
39
40 /* *INDENT-OFF* */
41 /* 16 octets */
42 typedef CLIB_PACKED (struct {
43   union
44     {
45       struct
46         {
47           ip4_address_t src;
48           ip4_address_t dst;
49           u16 src_port;
50           u16 dst_port;
51           /* align by making this 4 octets even though its a 1-bit field
52            * NOTE: avoid key overlap with other transports that use 5 tuples for
53            * session identification.
54            */
55           u32 proto;
56         };
57       u64 as_u64[2];
58     };
59 }) v4_connection_key_t;
60
61 typedef CLIB_PACKED (struct {
62   union
63     {
64       struct
65         {
66           /* 48 octets */
67           ip6_address_t src;
68           ip6_address_t dst;
69           u16 src_port;
70           u16 dst_port;
71           u32 proto;
72           u64 unused;
73         };
74       u64 as_u64[6];
75     };
76 }) v6_connection_key_t;
77 /* *INDENT-ON* */
78
79 typedef clib_bihash_kv_16_8_t session_kv4_t;
80 typedef clib_bihash_kv_48_8_t session_kv6_t;
81
82 always_inline void
83 make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
84                u16 lcl_port, u16 rmt_port, u8 proto)
85 {
86   kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32;
87   kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
88   kv->value = ~0ULL;
89 }
90
91 always_inline void
92 make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
93                      u8 proto)
94 {
95   kv->key[0] = (u64) lcl->as_u32;
96   kv->key[1] = (u64) proto << 32 | (u64) lcl_port;
97   kv->value = ~0ULL;
98 }
99
100 always_inline void
101 make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
102 {
103   kv->key[0] = (u64) lcl->as_u32;
104   kv->key[1] = (u64) proto << 32;
105   kv->value = ~0ULL;
106 }
107
108 always_inline void
109 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
110 {
111   make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
112                  tc->rmt_port, tc->proto);
113 }
114
115 always_inline void
116 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
117                u16 lcl_port, u16 rmt_port, u8 proto)
118 {
119   kv->key[0] = lcl->as_u64[0];
120   kv->key[1] = lcl->as_u64[1];
121   kv->key[2] = rmt->as_u64[0];
122   kv->key[3] = rmt->as_u64[1];
123   kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
124   kv->key[5] = 0;
125   kv->value = ~0ULL;
126 }
127
128 always_inline void
129 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
130                      u8 proto)
131 {
132   kv->key[0] = lcl->as_u64[0];
133   kv->key[1] = lcl->as_u64[1];
134   kv->key[2] = 0;
135   kv->key[3] = 0;
136   kv->key[4] = (u64) proto << 32 | (u64) lcl_port;
137   kv->key[5] = 0;
138   kv->value = ~0ULL;
139 }
140
141 always_inline void
142 make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
143 {
144   kv->key[0] = lcl->as_u64[0];
145   kv->key[1] = lcl->as_u64[1];
146   kv->key[2] = 0;
147   kv->key[3] = 0;
148   kv->key[4] = (u64) proto << 32;
149   kv->key[5] = 0;
150   kv->value = ~0ULL;
151 }
152
153 always_inline void
154 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
155 {
156   make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
157                  tc->rmt_port, tc->proto);
158 }
159
160 static inline u8
161 session_table_alloc_needs_sync (void)
162 {
163   return !vlib_thread_is_main_w_barrier () && (vlib_num_workers () > 1);
164 }
165
166 static session_table_t *
167 session_table_get_or_alloc (u8 fib_proto, u32 fib_index)
168 {
169   session_table_t *st;
170   u32 table_index;
171   ASSERT (fib_index != ~0);
172   if (vec_len (fib_index_to_table_index[fib_proto]) > fib_index &&
173       fib_index_to_table_index[fib_proto][fib_index] != ~0)
174     {
175       table_index = fib_index_to_table_index[fib_proto][fib_index];
176       return session_table_get (table_index);
177     }
178   else
179     {
180       u8 needs_sync = session_table_alloc_needs_sync ();
181       session_lookup_main_t *slm = &sl_main;
182
183       /* Stop workers, otherwise consumers might be affected. This is
184        * acceptable because new tables should seldom be allocated */
185       if (needs_sync)
186         {
187           vlib_workers_sync ();
188
189           /* We might have a race, only one worker allowed at once */
190           clib_spinlock_lock (&slm->st_alloc_lock);
191         }
192
193       st = session_table_alloc ();
194       table_index = session_table_index (st);
195       vec_validate_init_empty (fib_index_to_table_index[fib_proto], fib_index,
196                                ~0);
197       fib_index_to_table_index[fib_proto][fib_index] = table_index;
198       st->active_fib_proto = fib_proto;
199       session_table_init (st, fib_proto);
200
201       if (needs_sync)
202         {
203           clib_spinlock_unlock (&slm->st_alloc_lock);
204           vlib_workers_continue ();
205         }
206
207       return st;
208     }
209 }
210
211 static session_table_t *
212 session_table_get_or_alloc_for_connection (transport_connection_t * tc)
213 {
214   u32 fib_proto;
215   fib_proto = transport_connection_fib_proto (tc);
216   return session_table_get_or_alloc (fib_proto, tc->fib_index);
217 }
218
219 static session_table_t *
220 session_table_get_for_connection (transport_connection_t * tc)
221 {
222   u32 fib_proto = transport_connection_fib_proto (tc);
223   if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
224     return 0;
225   return
226     session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
227 }
228
229 static session_table_t *
230 session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
231 {
232   if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
233     return 0;
234   return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
235 }
236
237 u32
238 session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
239 {
240   if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
241     return SESSION_TABLE_INVALID_INDEX;
242   return fib_index_to_table_index[fib_proto][fib_index];
243 }
244
245 u32
246 session_lookup_get_or_alloc_index_for_fib (u32 fib_proto, u32 fib_index)
247 {
248   session_table_t *st;
249   st = session_table_get_or_alloc (fib_proto, fib_index);
250   return session_table_index (st);
251 }
252
253 /**
254  * Add transport connection to a session table
255  *
256  * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
257  * is added to requested session table.
258  *
259  * @param tc            transport connection to be added
260  * @param value         value to be stored
261  *
262  * @return non-zero if failure
263  */
264 int
265 session_lookup_add_connection (transport_connection_t * tc, u64 value)
266 {
267   session_table_t *st;
268   session_kv4_t kv4;
269   session_kv6_t kv6;
270
271   st = session_table_get_or_alloc_for_connection (tc);
272   if (!st)
273     return -1;
274   if (tc->is_ip4)
275     {
276       make_v4_ss_kv_from_tc (&kv4, tc);
277       kv4.value = value;
278       return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
279                                        1 /* is_add */ );
280     }
281   else
282     {
283       make_v6_ss_kv_from_tc (&kv6, tc);
284       kv6.value = value;
285       return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
286                                        1 /* is_add */ );
287     }
288 }
289
290 int
291 session_lookup_add_session_endpoint (u32 table_index,
292                                      session_endpoint_t * sep, u64 value)
293 {
294   session_table_t *st;
295   session_kv4_t kv4;
296   session_kv6_t kv6;
297
298   st = session_table_get (table_index);
299   if (!st)
300     return -1;
301   if (sep->is_ip4)
302     {
303       make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
304                            sep->transport_proto);
305       kv4.value = value;
306       return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
307     }
308   else
309     {
310       make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
311                            sep->transport_proto);
312       kv6.value = value;
313       return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
314     }
315 }
316
317 int
318 session_lookup_del_session_endpoint (u32 table_index,
319                                      session_endpoint_t * sep)
320 {
321   session_table_t *st;
322   session_kv4_t kv4;
323   session_kv6_t kv6;
324
325   st = session_table_get (table_index);
326   if (!st)
327     return -1;
328   if (sep->is_ip4)
329     {
330       make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
331                            sep->transport_proto);
332       return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
333     }
334   else
335     {
336       make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
337                            sep->transport_proto);
338       return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
339     }
340 }
341
342 int
343 session_lookup_del_session_endpoint2 (session_endpoint_t * sep)
344 {
345   fib_protocol_t fib_proto;
346   session_table_t *st;
347   session_kv4_t kv4;
348   session_kv6_t kv6;
349
350   fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
351   st = session_table_get_for_fib_index (fib_proto, sep->fib_index);
352   if (!st)
353     return -1;
354   if (sep->is_ip4)
355     {
356       make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
357                            sep->transport_proto);
358       return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
359     }
360   else
361     {
362       make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
363                            sep->transport_proto);
364       return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
365     }
366 }
367
368 /**
369  * Delete transport connection from session table
370  *
371  * @param table_index   session table index
372  * @param tc            transport connection to be removed
373  *
374  * @return non-zero if failure
375  */
376 int
377 session_lookup_del_connection (transport_connection_t * tc)
378 {
379   session_table_t *st;
380   session_kv4_t kv4;
381   session_kv6_t kv6;
382
383   st = session_table_get_for_connection (tc);
384   if (!st)
385     return -1;
386   if (tc->is_ip4)
387     {
388       make_v4_ss_kv_from_tc (&kv4, tc);
389       return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
390                                        0 /* is_add */ );
391     }
392   else
393     {
394       make_v6_ss_kv_from_tc (&kv6, tc);
395       return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
396                                        0 /* is_add */ );
397     }
398 }
399
400 int
401 session_lookup_del_session (session_t * s)
402 {
403   transport_connection_t *ts;
404   ts = transport_get_connection (session_get_transport_proto (s),
405                                  s->connection_index, s->thread_index);
406   if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
407     return 0;
408   return session_lookup_del_connection (ts);
409 }
410
411 static u8
412 session_lookup_action_index_is_valid (u32 action_index)
413 {
414   if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
415       || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
416     return 0;
417   return 1;
418 }
419
420 static u64
421 session_lookup_action_to_handle (u32 action_index)
422 {
423   switch (action_index)
424     {
425     case SESSION_RULES_TABLE_ACTION_DROP:
426       return SESSION_DROP_HANDLE;
427     case SESSION_RULES_TABLE_ACTION_ALLOW:
428     case SESSION_RULES_TABLE_INVALID_INDEX:
429       return SESSION_INVALID_HANDLE;
430     default:
431       /* application index */
432       return action_index;
433     }
434 }
435
436 static session_t *
437 session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
438                                    u8 transport_proto)
439 {
440   application_t *app;
441   app = application_get_if_valid (app_index);
442   if (!app)
443     return 0;
444
445   return app_worker_first_listener (application_get_default_worker (app),
446                                     fib_proto, transport_proto);
447 }
448
449 static session_t *
450 session_lookup_action_to_session (u32 action_index, u8 fib_proto,
451                                   u8 transport_proto)
452 {
453   u32 app_index;
454   app_index = session_lookup_action_to_handle (action_index);
455   /* Nothing sophisticated for now, action index is app index */
456   return session_lookup_app_listen_session (app_index, fib_proto,
457                                             transport_proto);
458 }
459
460 /** UNUSED */
461 session_t *
462 session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
463                                      ip4_address_t * lcl, u16 lcl_port,
464                                      ip4_address_t * rmt, u16 rmt_port)
465 {
466   session_rules_table_t *srt = &st->session_rules[proto];
467   u32 action_index, app_index;
468   action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
469                                               rmt_port);
470   app_index = session_lookup_action_to_handle (action_index);
471   /* Nothing sophisticated for now, action index is app index */
472   return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
473                                             proto);
474 }
475
476 /** UNUSED */
477 session_t *
478 session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
479                                      ip6_address_t * lcl, u16 lcl_port,
480                                      ip6_address_t * rmt, u16 rmt_port)
481 {
482   session_rules_table_t *srt = &st->session_rules[proto];
483   u32 action_index, app_index;
484   action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
485                                               rmt_port);
486   app_index = session_lookup_action_to_handle (action_index);
487   return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
488                                             proto);
489 }
490
491 /**
492  * Lookup listener for session endpoint in table
493  *
494  * @param table_index table where the endpoint should be looked up
495  * @param sep session endpoint to be looked up
496  * @param use_rules flag that indicates if the session rules of the table
497  *                  should be used
498  * @return invalid handle if nothing is found, the handle of a valid listener
499  *         or an action derived handle if a rule is hit
500  */
501 u64
502 session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
503                                   u8 use_rules)
504 {
505   session_rules_table_t *srt;
506   session_table_t *st;
507   u32 ai;
508   int rv;
509
510   st = session_table_get (table_index);
511   if (!st)
512     return SESSION_INVALID_HANDLE;
513   if (sep->is_ip4)
514     {
515       session_kv4_t kv4;
516       ip4_address_t lcl4;
517
518       make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
519                            sep->transport_proto);
520       rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
521       if (rv == 0)
522         return kv4.value;
523       if (use_rules)
524         {
525           clib_memset (&lcl4, 0, sizeof (lcl4));
526           srt = &st->session_rules[sep->transport_proto];
527           ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
528                                             sep->port);
529           if (session_lookup_action_index_is_valid (ai))
530             return session_lookup_action_to_handle (ai);
531         }
532     }
533   else
534     {
535       session_kv6_t kv6;
536       ip6_address_t lcl6;
537
538       make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
539                            sep->transport_proto);
540       rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
541       if (rv == 0)
542         return kv6.value;
543
544       if (use_rules)
545         {
546           clib_memset (&lcl6, 0, sizeof (lcl6));
547           srt = &st->session_rules[sep->transport_proto];
548           ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
549                                             sep->port);
550           if (session_lookup_action_index_is_valid (ai))
551             return session_lookup_action_to_handle (ai);
552         }
553     }
554   return SESSION_INVALID_HANDLE;
555 }
556
557 /**
558  * Look up endpoint in local session table
559  *
560  * The result, for now, is an application index and it may in the future
561  * be extended to a more complicated "action object". The only action we
562  * emulate now is "drop" and for that we return a special app index.
563  *
564  * Lookup logic is to check in order:
565  * - the rules in the table (connect acls)
566  * - session sub-table for a listener
567  * - session sub-table for a local listener (zeroed addr)
568  *
569  * @param table_index table where the lookup should be done
570  * @param sep session endpoint to be looked up
571  * @return session handle that can be interpreted as an adjacency
572  */
573 u64
574 session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
575 {
576   session_rules_table_t *srt;
577   session_table_t *st;
578   u32 ai;
579   int rv;
580
581   st = session_table_get (table_index);
582   if (!st)
583     return SESSION_INVALID_INDEX;
584   ASSERT (st->is_local);
585
586   if (sep->is_ip4)
587     {
588       session_kv4_t kv4;
589       ip4_address_t lcl4;
590
591       /*
592        * Check if endpoint has special rules associated
593        */
594       clib_memset (&lcl4, 0, sizeof (lcl4));
595       srt = &st->session_rules[sep->transport_proto];
596       ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
597                                         sep->port);
598       if (session_lookup_action_index_is_valid (ai))
599         return session_lookup_action_to_handle (ai);
600
601       /*
602        * Check if session endpoint is a listener
603        */
604       make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
605                            sep->transport_proto);
606       rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
607       if (rv == 0)
608         return kv4.value;
609
610       /*
611        * Zero out the ip. Logic is that connect to local ips, say
612        * 127.0.0.1:port, can match 0.0.0.0:port
613        */
614       if (ip4_is_local_host (&sep->ip.ip4))
615         {
616           kv4.key[0] = 0;
617           rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
618           if (rv == 0)
619             return kv4.value;
620         }
621       else
622         {
623           kv4.key[0] = 0;
624         }
625
626       /*
627        * Zero out the port and check if we have proxy
628        */
629       kv4.key[1] = 0;
630       rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
631       if (rv == 0)
632         return kv4.value;
633     }
634   else
635     {
636       session_kv6_t kv6;
637       ip6_address_t lcl6;
638
639       clib_memset (&lcl6, 0, sizeof (lcl6));
640       srt = &st->session_rules[sep->transport_proto];
641       ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
642                                         sep->port);
643       if (session_lookup_action_index_is_valid (ai))
644         return session_lookup_action_to_handle (ai);
645
646       make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
647                            sep->transport_proto);
648       rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
649       if (rv == 0)
650         return kv6.value;
651
652       /*
653        * Zero out the ip. Same logic as above.
654        */
655
656       if (ip6_is_local_host (&sep->ip.ip6))
657         {
658           kv6.key[0] = kv6.key[1] = 0;
659           rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
660           if (rv == 0)
661             return kv6.value;
662         }
663       else
664         {
665           kv6.key[0] = kv6.key[1] = 0;
666         }
667
668       /*
669        * Zero out the port. Same logic as above.
670        */
671       kv6.key[4] = kv6.key[5] = 0;
672       rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
673       if (rv == 0)
674         return kv6.value;
675     }
676   return SESSION_INVALID_HANDLE;
677 }
678
679 static inline session_t *
680 session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
681                             u16 lcl_port, u8 proto, u8 use_wildcard)
682 {
683   session_kv4_t kv4;
684   int rv;
685
686   /*
687    * First, try a fully formed listener
688    */
689   make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
690   rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
691   if (rv == 0)
692     return listen_session_get ((u32) kv4.value);
693
694   /*
695    * Zero out the lcl ip and check if any 0/0 port binds have been done
696    */
697   if (use_wildcard)
698     {
699       kv4.key[0] = 0;
700       rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
701       if (rv == 0)
702         return listen_session_get ((u32) kv4.value);
703     }
704   else
705     {
706       kv4.key[0] = 0;
707     }
708
709   /*
710    * Zero out port and check if we have a proxy set up for our ip
711    */
712   make_v4_proxy_kv (&kv4, lcl, proto);
713   rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
714   if (rv == 0)
715     return listen_session_get ((u32) kv4.value);
716
717   return 0;
718 }
719
720 session_t *
721 session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
722                           u8 proto, u8 use_wildcard)
723 {
724   session_table_t *st;
725   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
726   if (!st)
727     return 0;
728   return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard);
729 }
730
731 static session_t *
732 session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
733                             u16 lcl_port, u8 proto, u8 ip_wildcard)
734 {
735   session_kv6_t kv6;
736   int rv;
737
738   make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
739   rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
740   if (rv == 0)
741     return listen_session_get ((u32) kv6.value);
742
743   /* Zero out the lcl ip */
744   if (ip_wildcard)
745     {
746       kv6.key[0] = kv6.key[1] = 0;
747       rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
748       if (rv == 0)
749         return listen_session_get ((u32) kv6.value);
750     }
751   else
752     {
753       kv6.key[0] = kv6.key[1] = 0;
754     }
755
756   make_v6_proxy_kv (&kv6, lcl, proto);
757   rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
758   if (rv == 0)
759     return listen_session_get ((u32) kv6.value);
760   return 0;
761 }
762
763 session_t *
764 session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
765                           u8 proto, u8 use_wildcard)
766 {
767   session_table_t *st;
768   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
769   if (!st)
770     return 0;
771   return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard);
772 }
773
774 /**
775  * Lookup listener, exact or proxy (inaddr_any:0) match
776  */
777 session_t *
778 session_lookup_listener (u32 table_index, session_endpoint_t * sep)
779 {
780   session_table_t *st;
781   st = session_table_get (table_index);
782   if (!st)
783     return 0;
784   if (sep->is_ip4)
785     return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
786                                        sep->transport_proto, 0);
787   else
788     return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
789                                        sep->transport_proto, 0);
790   return 0;
791 }
792
793 /**
794  * Lookup listener wildcard match
795  */
796 session_t *
797 session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep)
798 {
799   session_table_t *st;
800   st = session_table_get (table_index);
801   if (!st)
802     return 0;
803   if (sep->is_ip4)
804     return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
805                                        sep->transport_proto,
806                                        1 /* use_wildcard */ );
807   else
808     return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
809                                        sep->transport_proto,
810                                        1 /* use_wildcard */ );
811   return 0;
812 }
813
814 int
815 session_lookup_add_half_open (transport_connection_t * tc, u64 value)
816 {
817   session_table_t *st;
818   session_kv4_t kv4;
819   session_kv6_t kv6;
820
821   st = session_table_get_or_alloc_for_connection (tc);
822   if (!st)
823     return 0;
824   if (tc->is_ip4)
825     {
826       make_v4_ss_kv_from_tc (&kv4, tc);
827       kv4.value = value;
828       return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
829                                        1 /* is_add */ );
830     }
831   else
832     {
833       make_v6_ss_kv_from_tc (&kv6, tc);
834       kv6.value = value;
835       return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
836                                        1 /* is_add */ );
837     }
838 }
839
840 int
841 session_lookup_del_half_open (transport_connection_t * tc)
842 {
843   session_table_t *st;
844   session_kv4_t kv4;
845   session_kv6_t kv6;
846
847   st = session_table_get_for_connection (tc);
848   if (!st)
849     return -1;
850   if (tc->is_ip4)
851     {
852       make_v4_ss_kv_from_tc (&kv4, tc);
853       return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
854                                        0 /* is_add */ );
855     }
856   else
857     {
858       make_v6_ss_kv_from_tc (&kv6, tc);
859       return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
860                                        0 /* is_add */ );
861     }
862 }
863
864 u64
865 session_lookup_half_open_handle (transport_connection_t * tc)
866 {
867   session_table_t *st;
868   session_kv4_t kv4;
869   session_kv6_t kv6;
870   int rv;
871
872   st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
873                                         tc->fib_index);
874   if (!st)
875     return HALF_OPEN_LOOKUP_INVALID_VALUE;
876   if (tc->is_ip4)
877     {
878       make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
879                      tc->rmt_port, tc->proto);
880       rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
881       if (rv == 0)
882         return kv4.value;
883     }
884   else
885     {
886       make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
887                      tc->rmt_port, tc->proto);
888       rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
889       if (rv == 0)
890         return kv6.value;
891     }
892   return HALF_OPEN_LOOKUP_INVALID_VALUE;
893 }
894
895 transport_connection_t *
896 session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
897 {
898   if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
899     {
900       u32 sst = session_type_from_proto_and_ip (proto, is_ip4);
901       return transport_get_half_open (sst, handle & 0xFFFFFFFF);
902     }
903   return 0;
904 }
905
906 /**
907  * Lookup connection with ip4 and transport layer information
908  *
909  * This is used on the fast path so it needs to be fast. Thereby,
910  * duplication of code and 'hacks' allowed.
911  *
912  * The lookup is incremental and returns whenever something is matched. The
913  * steps are:
914  * - Try to find an established session
915  * - Try to find a half-open connection
916  * - Try session rules table
917  * - Try to find a fully-formed or local source wildcarded (listener bound to
918  *   all interfaces) listener session
919  * - return 0
920  *
921  * @param fib_index     index of fib wherein the connection was received
922  * @param lcl           local ip4 address
923  * @param rmt           remote ip4 address
924  * @param lcl_port      local port
925  * @param rmt_port      remote port
926  * @param proto         transport protocol (e.g., tcp, udp)
927  * @param thread_index  thread index for request
928  * @param is_filtered   return flag that indicates if connection was filtered.
929  *
930  * @return pointer to transport connection, if one is found, 0 otherwise
931  */
932 transport_connection_t *
933 session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
934                                ip4_address_t * rmt, u16 lcl_port,
935                                u16 rmt_port, u8 proto, u32 thread_index,
936                                u8 * result)
937 {
938   session_table_t *st;
939   session_kv4_t kv4;
940   session_t *s;
941   u32 action_index;
942   int rv;
943
944   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
945   if (PREDICT_FALSE (!st))
946     return 0;
947
948   /*
949    * Lookup session amongst established ones
950    */
951   make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
952   rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
953   if (rv == 0)
954     {
955       if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index))
956         {
957           *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
958           return 0;
959         }
960       s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
961       return transport_get_connection (proto, s->connection_index,
962                                        thread_index);
963     }
964
965   /*
966    * Try half-open connections
967    */
968   rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
969   if (rv == 0)
970     return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
971
972   /*
973    * Check the session rules table
974    */
975   action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
976                                               rmt, lcl_port, rmt_port);
977   if (session_lookup_action_index_is_valid (action_index))
978     {
979       if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
980         {
981           *result = SESSION_LOOKUP_RESULT_FILTERED;
982           return 0;
983         }
984       if ((s = session_lookup_action_to_session (action_index,
985                                                  FIB_PROTOCOL_IP4, proto)))
986         return transport_get_listener (proto, s->connection_index);
987       return 0;
988     }
989
990   /*
991    * If nothing is found, check if any listener is available
992    */
993   s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
994   if (s)
995     return transport_get_listener (proto, s->connection_index);
996
997   return 0;
998 }
999
1000 /**
1001  * Lookup connection with ip4 and transport layer information
1002  *
1003  * Not optimized. Lookup logic is identical to that of
1004  * @ref session_lookup_connection_wt4
1005  *
1006  * @param fib_index     index of the fib wherein the connection was received
1007  * @param lcl           local ip4 address
1008  * @param rmt           remote ip4 address
1009  * @param lcl_port      local port
1010  * @param rmt_port      remote port
1011  * @param proto         transport protocol (e.g., tcp, udp)
1012  *
1013  * @return pointer to transport connection, if one is found, 0 otherwise
1014  */
1015 transport_connection_t *
1016 session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
1017                             ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
1018                             u8 proto)
1019 {
1020   session_table_t *st;
1021   session_kv4_t kv4;
1022   session_t *s;
1023   u32 action_index;
1024   int rv;
1025
1026   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1027   if (PREDICT_FALSE (!st))
1028     return 0;
1029
1030   /*
1031    * Lookup session amongst established ones
1032    */
1033   make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1034   rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1035   if (rv == 0)
1036     {
1037       s = session_get_from_handle (kv4.value);
1038       return transport_get_connection (proto, s->connection_index,
1039                                        s->thread_index);
1040     }
1041
1042   /*
1043    * Try half-open connections
1044    */
1045   rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
1046   if (rv == 0)
1047     return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
1048
1049   /*
1050    * Check the session rules table
1051    */
1052   action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1053                                               rmt, lcl_port, rmt_port);
1054   if (session_lookup_action_index_is_valid (action_index))
1055     {
1056       if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1057         return 0;
1058       if ((s = session_lookup_action_to_session (action_index,
1059                                                  FIB_PROTOCOL_IP4, proto)))
1060         return transport_get_listener (proto, s->connection_index);
1061       return 0;
1062     }
1063
1064   /*
1065    * If nothing is found, check if any listener is available
1066    */
1067   s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
1068   if (s)
1069     return transport_get_listener (proto, s->connection_index);
1070
1071   return 0;
1072 }
1073
1074 /**
1075  * Lookup session with ip4 and transport layer information
1076  *
1077  * Important note: this may look into another thread's pool table
1078  *
1079  * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
1080  * this returns a session as opposed to a transport connection and it does not
1081  * try to lookup half-open sessions.
1082  *
1083  * Typically used by dgram connections
1084  */
1085 session_t *
1086 session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1087                       u16 lcl_port, u16 rmt_port, u8 proto)
1088 {
1089   session_table_t *st;
1090   session_kv4_t kv4;
1091   session_t *s;
1092   u32 action_index;
1093   int rv;
1094
1095   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1096   if (PREDICT_FALSE (!st))
1097     return 0;
1098
1099   /*
1100    * Lookup session amongst established ones
1101    */
1102   make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1103   rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1104   if (rv == 0)
1105     return session_get_from_handle_safe (kv4.value);
1106
1107   /*
1108    * Check the session rules table
1109    */
1110   action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1111                                               rmt, lcl_port, rmt_port);
1112   if (session_lookup_action_index_is_valid (action_index))
1113     {
1114       if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1115         return 0;
1116       return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1117                                                proto);
1118     }
1119
1120   /*
1121    *  If nothing is found, check if any listener is available
1122    */
1123   if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1)))
1124     return s;
1125
1126   return 0;
1127 }
1128
1129 /**
1130  * Lookup connection with ip6 and transport layer information
1131  *
1132  * This is used on the fast path so it needs to be fast. Thereby,
1133  * duplication of code and 'hacks' allowed.
1134  *
1135  * The lookup is incremental and returns whenever something is matched. The
1136  * steps are:
1137  * - Try to find an established session
1138  * - Try to find a half-open connection
1139  * - Try session rules table
1140  * - Try to find a fully-formed or local source wildcarded (listener bound to
1141  *   all interfaces) listener session
1142  * - return 0
1143  *
1144  * @param fib_index     index of the fib wherein the connection was received
1145  * @param lcl           local ip6 address
1146  * @param rmt           remote ip6 address
1147  * @param lcl_port      local port
1148  * @param rmt_port      remote port
1149  * @param proto         transport protocol (e.g., tcp, udp)
1150  * @param thread_index  thread index for request
1151  *
1152  * @return pointer to transport connection, if one is found, 0 otherwise
1153  */
1154 transport_connection_t *
1155 session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1156                                ip6_address_t * rmt, u16 lcl_port,
1157                                u16 rmt_port, u8 proto, u32 thread_index,
1158                                u8 * result)
1159 {
1160   session_table_t *st;
1161   session_t *s;
1162   session_kv6_t kv6;
1163   u32 action_index;
1164   int rv;
1165
1166   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1167   if (PREDICT_FALSE (!st))
1168     return 0;
1169
1170   make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1171   rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1172   if (rv == 0)
1173     {
1174       ASSERT ((u32) (kv6.value >> 32) == thread_index);
1175       if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index))
1176         {
1177           *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
1178           return 0;
1179         }
1180       s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
1181       return transport_get_connection (proto, s->connection_index,
1182                                        thread_index);
1183     }
1184
1185   /* Try half-open connections */
1186   rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1187   if (rv == 0)
1188     return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
1189
1190   /* Check the session rules table */
1191   action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1192                                               rmt, lcl_port, rmt_port);
1193   if (session_lookup_action_index_is_valid (action_index))
1194     {
1195       if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1196         {
1197           *result = SESSION_LOOKUP_RESULT_FILTERED;
1198           return 0;
1199         }
1200       if ((s = session_lookup_action_to_session (action_index,
1201                                                  FIB_PROTOCOL_IP6, proto)))
1202         return transport_get_listener (proto, s->connection_index);
1203       return 0;
1204     }
1205
1206   /* If nothing is found, check if any listener is available */
1207   s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
1208   if (s)
1209     return transport_get_listener (proto, s->connection_index);
1210
1211   return 0;
1212 }
1213
1214 /**
1215  * Lookup connection with ip6 and transport layer information
1216  *
1217  * Not optimized. This is used on the fast path so it needs to be fast.
1218  * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1219  * to that of @ref session_lookup_connection_wt4
1220  *
1221  * @param fib_index     index of the fib wherein the connection was received
1222  * @param lcl           local ip6 address
1223  * @param rmt           remote ip6 address
1224  * @param lcl_port      local port
1225  * @param rmt_port      remote port
1226  * @param proto         transport protocol (e.g., tcp, udp)
1227  *
1228  * @return pointer to transport connection, if one is found, 0 otherwise
1229  */
1230 transport_connection_t *
1231 session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1232                             ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1233                             u8 proto)
1234 {
1235   session_table_t *st;
1236   session_t *s;
1237   session_kv6_t kv6;
1238   u32 action_index;
1239   int rv;
1240
1241   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1242   if (PREDICT_FALSE (!st))
1243     return 0;
1244
1245   make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1246   rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1247   if (rv == 0)
1248     {
1249       s = session_get_from_handle (kv6.value);
1250       return transport_get_connection (proto, s->connection_index,
1251                                        s->thread_index);
1252     }
1253
1254   /* Try half-open connections */
1255   rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1256   if (rv == 0)
1257     return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
1258
1259   /* Check the session rules table */
1260   action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1261                                               rmt, lcl_port, rmt_port);
1262   if (session_lookup_action_index_is_valid (action_index))
1263     {
1264       if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1265         return 0;
1266       if ((s = session_lookup_action_to_session (action_index,
1267                                                  FIB_PROTOCOL_IP6, proto)))
1268         return transport_get_listener (proto, s->connection_index);
1269       return 0;
1270     }
1271
1272   /* If nothing is found, check if any listener is available */
1273   s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
1274   if (s)
1275     return transport_get_listener (proto, s->connection_index);
1276
1277   return 0;
1278 }
1279
1280 /**
1281  * Lookup session with ip6 and transport layer information
1282  *
1283  * Important note: this may look into another thread's pool table and
1284  * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1285  * if needed as soon as possible.
1286  *
1287  * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1288  * this returns a session as opposed to a transport connection and it does not
1289  * try to lookup half-open sessions.
1290  *
1291  * Typically used by dgram connections
1292  */
1293 session_t *
1294 session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1295                       u16 lcl_port, u16 rmt_port, u8 proto)
1296 {
1297   session_table_t *st;
1298   session_kv6_t kv6;
1299   session_t *s;
1300   u32 action_index;
1301   int rv;
1302
1303   st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1304   if (PREDICT_FALSE (!st))
1305     return 0;
1306
1307   make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1308   rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1309   if (rv == 0)
1310     return session_get_from_handle_safe (kv6.value);
1311
1312   /* Check the session rules table */
1313   action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1314                                               rmt, lcl_port, rmt_port);
1315   if (session_lookup_action_index_is_valid (action_index))
1316     {
1317       if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1318         return 0;
1319       return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1320                                                proto);
1321     }
1322
1323   /* If nothing is found, check if any listener is available */
1324   if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
1325     return s;
1326   return 0;
1327 }
1328
1329 transport_connection_t *
1330 session_lookup_connection (u32 fib_index, ip46_address_t * lcl,
1331                            ip46_address_t * rmt, u16 lcl_port, u16 rmt_port,
1332                            u8 proto, u8 is_ip4)
1333 {
1334   if (is_ip4)
1335     return session_lookup_connection4 (fib_index, &lcl->ip4, &rmt->ip4,
1336                                        lcl_port, rmt_port, proto);
1337   else
1338     return session_lookup_connection6 (fib_index, &lcl->ip6, &rmt->ip6,
1339                                        lcl_port, rmt_port, proto);
1340 }
1341
1342 session_error_t
1343 vnet_session_rule_add_del (session_rule_add_del_args_t *args)
1344 {
1345   app_namespace_t *app_ns = app_namespace_get (args->appns_index);
1346   session_rules_table_t *srt;
1347   session_table_t *st;
1348   u32 fib_index;
1349   u8 fib_proto;
1350   int rv = 0;
1351
1352   if (!app_ns)
1353     return SESSION_E_INVALID_NS;
1354
1355   if (args->scope > 3)
1356     return SESSION_E_INVALID;
1357
1358   if (args->transport_proto != TRANSPORT_PROTO_TCP
1359       && args->transport_proto != TRANSPORT_PROTO_UDP)
1360     return SESSION_E_INVALID;
1361
1362   if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1363     {
1364       fib_proto = args->table_args.rmt.fp_proto;
1365       fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1366       st = session_table_get_for_fib_index (fib_proto, fib_index);
1367       srt = &st->session_rules[args->transport_proto];
1368       if ((rv = session_rules_table_add_del (srt, &args->table_args)))
1369         return rv;
1370     }
1371   if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1372     {
1373       clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1374       args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1375       args->table_args.lcl_port = 0;
1376       st = app_namespace_get_local_table (app_ns);
1377       srt = &st->session_rules[args->transport_proto];
1378       rv = session_rules_table_add_del (srt, &args->table_args);
1379     }
1380   return rv;
1381 }
1382
1383 /**
1384  * Mark (global) tables as pertaining to app ns
1385  */
1386 void
1387 session_lookup_set_tables_appns (app_namespace_t * app_ns)
1388 {
1389   session_table_t *st;
1390   u32 fib_index;
1391   u8 fp;
1392
1393   for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1394     {
1395       fib_index = app_namespace_get_fib_index (app_ns, fp);
1396       st = session_table_get_or_alloc (fp, fib_index);
1397       if (st)
1398         st->appns_index = app_namespace_index (app_ns);
1399     }
1400 }
1401
1402 u8 *
1403 format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1404 {
1405   clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1406   u32 is_local = va_arg (*args, u32);
1407   v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1408   session_t *session;
1409   app_worker_t *app_wrk;
1410   const u8 *app_name;
1411   u8 *str = 0;
1412
1413   if (!is_local)
1414     {
1415       session = session_get_from_handle (kvp->value);
1416       app_wrk = app_worker_get (session->app_wrk_index);
1417       app_name = application_name_from_index (app_wrk->app_index);
1418       str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1419                     key->proto, format_ip4_address, &key->src,
1420                     clib_net_to_host_u16 (key->src_port), format_ip4_address,
1421                     &key->dst, clib_net_to_host_u16 (key->dst_port));
1422       s = format (s, "%-40v%-30v", str, app_name);
1423     }
1424   else
1425     {
1426       session = session_get_from_handle (kvp->value);
1427       app_wrk = app_worker_get (session->app_wrk_index);
1428       app_name = application_name_from_index (app_wrk->app_index);
1429       str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1430                     format_ip4_address, &key->src,
1431                     clib_net_to_host_u16 (key->src_port));
1432       s = format (s, "%-30v%-30v", str, app_name);
1433     }
1434   return s;
1435 }
1436
1437 typedef struct _ip4_session_table_show_ctx_t
1438 {
1439   vlib_main_t *vm;
1440   u8 is_local;
1441 } ip4_session_table_show_ctx_t;
1442
1443 static int
1444 ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1445 {
1446   ip4_session_table_show_ctx_t *ctx = arg;
1447   vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1448                    ctx->is_local);
1449   return 1;
1450 }
1451
1452 void
1453 session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1454                                    u8 type, u8 is_local)
1455 {
1456   ip4_session_table_show_ctx_t ctx = {
1457     .vm = vm,
1458     .is_local = is_local,
1459   };
1460   if (!is_local)
1461     vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1462   else
1463     vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1464   switch (type)
1465     {
1466       /* main table v4 */
1467     case 0:
1468       ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1469                               &ctx);
1470       break;
1471     default:
1472       clib_warning ("not supported");
1473     }
1474 }
1475
1476 static clib_error_t *
1477 session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1478                          vlib_cli_command_t * cmd)
1479 {
1480   u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
1481   clib_error_t *error = 0;
1482   u32 appns_index, scope = 0;
1483   ip46_address_t lcl_ip, rmt_ip;
1484   u8 is_ip4 = 1, conn_set = 0;
1485   u8 fib_proto, is_add = 1, *ns_id = 0;
1486   u8 *tag = 0;
1487   app_namespace_t *app_ns;
1488   int rv;
1489
1490   session_cli_return_if_not_enabled ();
1491
1492   clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1493   clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
1494   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1495     {
1496       if (unformat (input, "del"))
1497         is_add = 0;
1498       else if (unformat (input, "add"))
1499         ;
1500       else if (unformat (input, "appns %_%v%_", &ns_id))
1501         ;
1502       else if (unformat (input, "scope global"))
1503         scope = SESSION_RULE_SCOPE_GLOBAL;
1504       else if (unformat (input, "scope local"))
1505         scope = SESSION_RULE_SCOPE_LOCAL;
1506       else if (unformat (input, "scope all"))
1507         scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1508       else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1509         ;
1510       else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1511                          &lcl_ip.ip4, &lcl_plen, &lcl_port,
1512                          unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1513                          &rmt_port))
1514         {
1515           is_ip4 = 1;
1516           conn_set = 1;
1517         }
1518       else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1519                          &lcl_ip.ip6, &lcl_plen, &lcl_port,
1520                          unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1521                          &rmt_port))
1522         {
1523           is_ip4 = 0;
1524           conn_set = 1;
1525         }
1526       else if (unformat (input, "action %d", &action))
1527         ;
1528       else if (unformat (input, "tag %_%v%_", &tag))
1529         ;
1530       else
1531         {
1532           error = clib_error_return (0, "unknown input `%U'",
1533                                      format_unformat_error, input);
1534           goto done;
1535         }
1536     }
1537
1538   if (proto == ~0)
1539     {
1540       vlib_cli_output (vm, "proto must be set");
1541       goto done;
1542     }
1543   if (is_add && !conn_set && action == ~0)
1544     {
1545       vlib_cli_output (vm, "connection and action must be set for add");
1546       goto done;
1547     }
1548   if (!is_add && !tag && !conn_set)
1549     {
1550       vlib_cli_output (vm, "connection or tag must be set for delete");
1551       goto done;
1552     }
1553   if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1554     {
1555       vlib_cli_output (vm, "tag too long (max u64)");
1556       goto done;
1557     }
1558
1559   if (ns_id)
1560     {
1561       app_ns = app_namespace_get_from_id (ns_id);
1562       if (!app_ns)
1563         {
1564           vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1565           goto done;
1566         }
1567     }
1568   else
1569     {
1570       app_ns = app_namespace_get_default ();
1571     }
1572   appns_index = app_namespace_index (app_ns);
1573
1574   fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1575   session_rule_add_del_args_t args = {
1576     .transport_proto = proto,
1577     .table_args.lcl.fp_addr = lcl_ip,
1578     .table_args.lcl.fp_len = lcl_plen,
1579     .table_args.lcl.fp_proto = fib_proto,
1580     .table_args.rmt.fp_addr = rmt_ip,
1581     .table_args.rmt.fp_len = rmt_plen,
1582     .table_args.rmt.fp_proto = fib_proto,
1583     .table_args.lcl_port = lcl_port,
1584     .table_args.rmt_port = rmt_port,
1585     .table_args.action_index = action,
1586     .table_args.is_add = is_add,
1587     .table_args.tag = tag,
1588     .appns_index = appns_index,
1589     .scope = scope,
1590   };
1591   if ((rv = vnet_session_rule_add_del (&args)))
1592     error = clib_error_return (0, "rule add del returned %u", rv);
1593
1594 done:
1595   vec_free (ns_id);
1596   vec_free (tag);
1597   return error;
1598 }
1599
1600 /* *INDENT-OFF* */
1601 VLIB_CLI_COMMAND (session_rule_command, static) =
1602 {
1603   .path = "session rule",
1604   .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1605       "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1606   .function = session_rule_command_fn,
1607 };
1608 /* *INDENT-ON* */
1609
1610 void
1611 session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1612                                  u8 transport_proto)
1613 {
1614   vlib_main_t *vm = vlib_get_main ();
1615   session_rules_table_t *srt;
1616   session_table_t *st;
1617   st = session_table_get_for_fib_index (fib_index, fib_proto);
1618   srt = &st->session_rules[transport_proto];
1619   session_rules_table_cli_dump (vm, srt, fib_proto);
1620 }
1621
1622 void
1623 session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1624                                        u8 transport_proto)
1625 {
1626   vlib_main_t *vm = vlib_get_main ();
1627   session_rules_table_t *srt;
1628   session_table_t *st;
1629   st = session_table_get (table_index);
1630   srt = &st->session_rules[transport_proto];
1631   session_rules_table_cli_dump (vm, srt, fib_proto);
1632 }
1633
1634 static clib_error_t *
1635 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1636                                vlib_cli_command_t * cmd)
1637 {
1638   u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1639   u32 fib_index, scope = 0;
1640   ip46_address_t lcl_ip, rmt_ip;
1641   u8 is_ip4 = 1, show_one = 0;
1642   app_namespace_t *app_ns;
1643   session_rules_table_t *srt;
1644   session_table_t *st;
1645   u8 *ns_id = 0, fib_proto;
1646
1647   session_cli_return_if_not_enabled ();
1648
1649   clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1650   clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
1651   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1652     {
1653       if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1654         ;
1655       else if (unformat (input, "appns %_%v%_", &ns_id))
1656         ;
1657       else if (unformat (input, "scope global"))
1658         scope = 1;
1659       else if (unformat (input, "scope local"))
1660         scope = 2;
1661       else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1662                          &lcl_ip.ip4, &lcl_plen, &lcl_port,
1663                          unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1664                          &rmt_port))
1665         {
1666           is_ip4 = 1;
1667           show_one = 1;
1668         }
1669       else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1670                          &lcl_ip.ip6, &lcl_plen, &lcl_port,
1671                          unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1672                          &rmt_port))
1673         {
1674           is_ip4 = 0;
1675           show_one = 1;
1676         }
1677       else
1678         return clib_error_return (0, "unknown input `%U'",
1679                                   format_unformat_error, input);
1680     }
1681
1682   if (transport_proto == ~0)
1683     {
1684       vlib_cli_output (vm, "transport proto must be set");
1685       return 0;
1686     }
1687
1688   if (ns_id)
1689     {
1690       app_ns = app_namespace_get_from_id (ns_id);
1691       if (!app_ns)
1692         {
1693           vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1694           return 0;
1695         }
1696     }
1697   else
1698     {
1699       app_ns = app_namespace_get_default ();
1700     }
1701
1702   if (scope == 1 || scope == 0)
1703     {
1704       fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1705       fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1706       st = session_table_get_for_fib_index (fib_proto, fib_index);
1707     }
1708   else
1709     {
1710       st = app_namespace_get_local_table (app_ns);
1711     }
1712
1713   if (show_one)
1714     {
1715       srt = &st->session_rules[transport_proto];
1716       session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1717                                      rmt_port, is_ip4);
1718       return 0;
1719     }
1720
1721   vlib_cli_output (vm, "%U rules table", format_transport_proto,
1722                    transport_proto);
1723   srt = &st->session_rules[transport_proto];
1724   session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1725   session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
1726
1727   vec_free (ns_id);
1728   return 0;
1729 }
1730
1731 /* *INDENT-OFF* */
1732 VLIB_CLI_COMMAND (show_session_rules_command, static) =
1733 {
1734   .path = "show session rules",
1735   .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1736       "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
1737   .function = show_session_rules_command_fn,
1738 };
1739 /* *INDENT-ON* */
1740
1741 u8 *
1742 format_session_lookup_tables (u8 *s, va_list *args)
1743 {
1744   u32 fib_proto = va_arg (*args, u32);
1745   u32 *fibs, num_fibs = 0, fib_index, indent;
1746   session_table_t *st;
1747   u64 total_mem = 0;
1748
1749   fibs = fib_index_to_table_index[fib_proto];
1750
1751   for (fib_index = 0; fib_index < vec_len (fibs); fib_index++)
1752     {
1753       if (fibs[fib_index] == ~0)
1754         continue;
1755
1756       num_fibs += 1;
1757       st = session_table_get (fibs[fib_index]);
1758       total_mem += session_table_memory_size (st);
1759     }
1760
1761   indent = format_get_indent (s);
1762   s = format (s, "active fibs:\t%u\n", num_fibs);
1763   s = format (s, "%Umax fib-index:\t%u\n", format_white_space, indent,
1764               vec_len (fibs) - 1);
1765   s = format (s, "%Utable memory:\t%U\n", format_white_space, indent,
1766               format_memory_size, total_mem);
1767   s = format (s, "%Uvec memory:\t%U\n", format_white_space, indent,
1768               format_memory_size, vec_mem_size (fibs));
1769
1770   return s;
1771 }
1772
1773 static clib_error_t *
1774 show_session_lookup_command_fn (vlib_main_t *vm, unformat_input_t *input,
1775                                 vlib_cli_command_t *cmd)
1776 {
1777   session_table_t *st;
1778   u32 fib_index = ~0;
1779
1780   session_cli_return_if_not_enabled ();
1781   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1782     {
1783       if (unformat (input, "table %u", &fib_index))
1784         ;
1785       else
1786         return clib_error_return (0, "unknown input `%U'",
1787                                   format_unformat_error, input);
1788     }
1789
1790   if (fib_index != ~0)
1791     {
1792       st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1793       if (st)
1794         vlib_cli_output (vm, "%U", format_session_table, st);
1795       else
1796         vlib_cli_output (vm, "no ip4 table for fib-index %u", fib_index);
1797       st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1798       if (st)
1799         vlib_cli_output (vm, "%U", format_session_table, st);
1800       else
1801         vlib_cli_output (vm, "no ip6 table for fib-index %u", fib_index);
1802       goto done;
1803     }
1804
1805   vlib_cli_output (vm, "ip4 fib lookup tables:\n %U",
1806                    format_session_lookup_tables, FIB_PROTOCOL_IP4);
1807   vlib_cli_output (vm, "ip6 fib lookup tables:\n %U",
1808                    format_session_lookup_tables, FIB_PROTOCOL_IP6);
1809
1810 done:
1811   return 0;
1812 }
1813
1814 VLIB_CLI_COMMAND (show_session_lookup_command, static) = {
1815   .path = "show session lookup",
1816   .short_help = "show session lookup [table <fib-index>]",
1817   .function = show_session_lookup_command_fn,
1818 };
1819
1820 void
1821 session_lookup_init (void)
1822 {
1823   session_lookup_main_t *slm = &sl_main;
1824
1825   clib_spinlock_init (&slm->st_alloc_lock);
1826
1827   /*
1828    * Allocate default table and map it to fib_index 0
1829    */
1830   session_table_t *st = session_table_alloc ();
1831   vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1832   fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
1833   st->active_fib_proto = FIB_PROTOCOL_IP4;
1834   session_table_init (st, FIB_PROTOCOL_IP4);
1835   st = session_table_alloc ();
1836   vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1837   fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
1838   st->active_fib_proto = FIB_PROTOCOL_IP6;
1839   session_table_init (st, FIB_PROTOCOL_IP6);
1840 }
1841
1842 /*
1843  * fd.io coding-style-patch-verification: ON
1844  *
1845  * Local Variables:
1846  * eval: (c-set-style "gnu")
1847  * End:
1848  */