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