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