blob: abe2a2bb7afae440f4fb8028e2f9a83588156824 [file] [log] [blame]
Florin Coras1c710452017-10-17 00:03:13 -07001/*
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#include <vnet/session/mma_16.h>
17#include <vnet/session/mma_template.c>
18#include <vnet/session/mma_40.h>
19#include <vnet/session/mma_template.c>
20#include <vnet/session/session_rules_table.h>
21#include <vnet/session/transport.h>
22
Florin Corasc97a7392017-11-05 23:07:07 -080023u32
24session_rule_tag_key_index (u32 ri, u8 is_ip4)
25{
26 return ((ri << 1) | is_ip4);
27}
28
29void
30session_rule_tag_key_index_parse (u32 rti_key, u32 * ri, u8 * is_ip4)
31{
32 *is_ip4 = rti_key & 1;
33 *ri = rti_key >> 1;
34}
35
36u8 *
37session_rules_table_rule_tag (session_rules_table_t * srt, u32 ri, u8 is_ip4)
38{
39 uword *tip;
40 session_rule_tag_t *rt;
41
42 tip =
43 hash_get (srt->tags_by_rules, session_rule_tag_key_index (ri, is_ip4));
44 if (tip)
45 {
46 rt = pool_elt_at_index (srt->rule_tags, *tip);
47 return rt->tag;
48 }
49 return 0;
50}
51
52void
53session_rules_table_add_del_tag (session_rules_table_t * srt, u8 * tag,
54 u32 rule_index, u8 is_ip4, u8 is_add)
55{
56 uword *rip, *rtip;
57 session_rule_tag_t *rt;
58 u32 rti_key;
59
60 if (tag == 0)
61 return;
62 if (is_add)
63 {
64 pool_get (srt->rule_tags, rt);
65 rt->tag = vec_dup (tag);
66 hash_set_mem (srt->rules_by_tag, rt->tag, rule_index);
67 rti_key = session_rule_tag_key_index (rule_index, is_ip4);
68 hash_set (srt->tags_by_rules, rti_key, rt - srt->rule_tags);
69 }
70 else
71 {
72 rip = hash_get_mem (srt->rules_by_tag, tag);
73 if (!rip)
74 {
75 clib_warning ("tag has no rule associated");
76 return;
77 }
78 rti_key = session_rule_tag_key_index (*rip, is_ip4);
79 rtip = hash_get (srt->tags_by_rules, rti_key);
80 if (!rtip)
81 {
82 clib_warning ("rule has no tag associated");
83 return;
84 }
85 rt = pool_elt_at_index (srt->rule_tags, *rtip);
86 ASSERT (rt);
87 hash_unset_mem (srt->rules_by_tag, tag);
88 hash_unset (srt->tags_by_rules, rti_key);
89 pool_put (srt->rule_tags, rt);
90 }
91}
92
93u32
94session_rules_table_rule_for_tag (session_rules_table_t * srt, u8 * tag)
95{
96 uword *rp;
97 if (tag == 0)
98 return SESSION_RULES_TABLE_INVALID_INDEX;
99 rp = hash_get_mem (srt->rules_by_tag, tag);
100 return (rp == 0 ? SESSION_RULES_TABLE_INVALID_INDEX : *rp);
101}
102
Florin Coras1c710452017-10-17 00:03:13 -0700103static void
104fib_pref_normalize (fib_prefix_t * pref)
105{
106 if (pref->fp_proto == FIB_PROTOCOL_IP4)
107 ip4_address_normalize (&pref->fp_addr.ip4, pref->fp_len);
108 else
109 ip6_address_normalize (&pref->fp_addr.ip6, pref->fp_len);
110}
111
112u8 *
113format_session_rule4 (u8 * s, va_list * args)
114{
Florin Corasc97a7392017-11-05 23:07:07 -0800115 session_rules_table_t *srt = va_arg (*args, session_rules_table_t *);
Florin Coras1c710452017-10-17 00:03:13 -0700116 mma_rule_16_t *sr = va_arg (*args, mma_rule_16_t *);
117 session_mask_or_match_4_t *mask, *match;
Florin Corasc97a7392017-11-05 23:07:07 -0800118 mma_rules_table_16_t *srt4;
119 u8 *tag = 0, *null_tag = format (0, "none");
120 u32 ri;
Florin Coras1c710452017-10-17 00:03:13 -0700121 int i;
122
Florin Corasc97a7392017-11-05 23:07:07 -0800123 srt4 = &srt->session_rules_tables_16;
124 ri = mma_rules_table_rule_index_16 (srt4, sr);
125 tag = session_rules_table_rule_tag (srt, ri, 1);
Florin Coras1c710452017-10-17 00:03:13 -0700126 match = (session_mask_or_match_4_t *) & sr->match;
127 mask = (session_mask_or_match_4_t *) & sr->mask;
128
Florin Corasc97a7392017-11-05 23:07:07 -0800129 s = format (s, "[%d] rule: %U/%d %d %U/%d %d action: %d tag: %v", ri,
130 format_ip4_address, &match->lcl_ip,
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100131 ip4_mask_to_preflen (&mask->lcl_ip),
132 clib_net_to_host_u16 (match->lcl_port), format_ip4_address,
133 &match->rmt_ip, ip4_mask_to_preflen (&mask->rmt_ip),
134 clib_net_to_host_u16 (match->rmt_port), sr->action_index,
135 tag ? tag : null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700136 if (vec_len (sr->next_indices))
137 {
138 s = format (s, "\n children: ");
139 for (i = 0; i < vec_len (sr->next_indices); i++)
140 s = format (s, "%d ", sr->next_indices[i]);
141 }
Florin Corasc97a7392017-11-05 23:07:07 -0800142 vec_free (null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700143 return s;
144}
145
146u8 *
147format_session_rule6 (u8 * s, va_list * args)
148{
Florin Corasc97a7392017-11-05 23:07:07 -0800149 session_rules_table_t *srt = va_arg (*args, session_rules_table_t *);
Florin Coras1c710452017-10-17 00:03:13 -0700150 mma_rule_40_t *sr = va_arg (*args, mma_rule_40_t *);
151 session_mask_or_match_6_t *mask, *match;
Florin Corasc97a7392017-11-05 23:07:07 -0800152 mma_rules_table_40_t *srt6;
153 u8 *tag = 0, *null_tag = format (0, "none");
154 u32 ri;
Florin Coras1c710452017-10-17 00:03:13 -0700155 int i;
156
Florin Corasc97a7392017-11-05 23:07:07 -0800157 srt6 = &srt->session_rules_tables_40;
158 ri = mma_rules_table_rule_index_40 (srt6, sr);
159 tag = session_rules_table_rule_tag (srt, ri, 0);
Florin Coras1c710452017-10-17 00:03:13 -0700160 match = (session_mask_or_match_6_t *) & sr->match;
161 mask = (session_mask_or_match_6_t *) & sr->mask;
162
Florin Corasc97a7392017-11-05 23:07:07 -0800163 s = format (s, "[%d] rule: %U/%d %d %U/%d %d action: %d tag: %v", ri,
164 format_ip6_address, &match->lcl_ip,
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100165 ip6_mask_to_preflen (&mask->lcl_ip),
166 clib_net_to_host_u16 (match->lcl_port), format_ip6_address,
167 &match->rmt_ip, ip6_mask_to_preflen (&mask->rmt_ip),
168 clib_net_to_host_u16 (match->rmt_port), sr->action_index,
169 tag ? tag : null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700170 if (vec_len (sr->next_indices))
171 {
172 s = format (s, "\n children: ");
173 for (i = 0; i < vec_len (sr->next_indices); i++)
174 s = format (s, "%d ", sr->next_indices[i]);
175 }
Florin Corasc97a7392017-11-05 23:07:07 -0800176 vec_free (null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700177 return s;
178}
179
180void *
Florin Corasc97a7392017-11-05 23:07:07 -0800181session_rules_table_get (session_rules_table_t * srt, u8 fib_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700182{
183 if (fib_proto == FIB_PROTOCOL_IP4)
Florin Corasc97a7392017-11-05 23:07:07 -0800184 return &srt->session_rules_tables_16;
Florin Coras1c710452017-10-17 00:03:13 -0700185 else if (fib_proto == FIB_PROTOCOL_IP6)
Florin Corasc97a7392017-11-05 23:07:07 -0800186 return &srt->session_rules_tables_40;
Florin Coras1c710452017-10-17 00:03:13 -0700187 return 0;
188}
189
190int
191rule_cmp_16 (mma_rule_16_t * rule1, mma_rule_16_t * rule2)
192{
193 session_mask_or_match_4_t *m1, *m2;
194
195 m1 = (session_mask_or_match_4_t *) & rule1->max_match;
196 m2 = (session_mask_or_match_4_t *) & rule2->max_match;
197 if (m1->rmt_ip.as_u32 != m2->rmt_ip.as_u32)
198 return (m1->rmt_ip.as_u32 < m2->rmt_ip.as_u32 ? -1 : 1);
199 if (m1->lcl_ip.as_u32 != m2->lcl_ip.as_u32)
200 return (m1->lcl_ip.as_u32 < m2->lcl_ip.as_u32 ? -1 : 1);
201 if (m1->rmt_port != m2->rmt_port)
202 return (m1->rmt_port < m2->rmt_port ? -1 : 1);
203 if (m1->lcl_port != m2->lcl_port)
204 return (m1->lcl_port < m2->lcl_port ? -1 : 1);
205 return 0;
206}
207
208int
209rule_cmp_40 (mma_rule_40_t * rule1, mma_rule_40_t * rule2)
210{
211 session_mask_or_match_6_t *r1, *r2;
212 r1 = (session_mask_or_match_6_t *) & rule1->max_match;
213 r2 = (session_mask_or_match_6_t *) & rule2->max_match;
214 if (r1->rmt_ip.as_u64[0] != r2->rmt_ip.as_u64[0])
215 return (r1->rmt_ip.as_u64[0] < r2->rmt_ip.as_u64[0] ? -1 : 1);
216 if (r1->rmt_ip.as_u64[1] != r2->rmt_ip.as_u64[1])
217 return (r1->rmt_ip.as_u64[1] < r2->rmt_ip.as_u64[1] ? -1 : 1);
218 if (r1->lcl_ip.as_u64[0] != r2->lcl_ip.as_u64[0])
219 return (r1->lcl_ip.as_u64[0] < r2->lcl_ip.as_u64[0] ? -1 : 1);
220 if (r1->lcl_ip.as_u64[1] != r2->lcl_ip.as_u64[1])
221 return (r1->lcl_ip.as_u64[1] < r2->lcl_ip.as_u64[1]) ? -1 : 1;
222 if (r1->rmt_port != r2->rmt_port)
223 return (r1->rmt_port < r2->rmt_port ? -1 : 1);
224 if (r1->lcl_port != r2->lcl_port)
225 return (r1->lcl_port < r2->lcl_port ? -1 : 1);
226 return 0;
227}
228
229void
230session_rules_table_init_rule_16 (mma_rule_16_t * rule,
231 fib_prefix_t * lcl, u16 lcl_port,
232 fib_prefix_t * rmt, u16 rmt_port)
233{
234 session_mask_or_match_4_t *match, *mask, *max_match;
235 fib_pref_normalize (lcl);
236 fib_pref_normalize (rmt);
237 match = (session_mask_or_match_4_t *) & rule->match;
238 match->lcl_ip.as_u32 = lcl->fp_addr.ip4.as_u32;
239 match->rmt_ip.as_u32 = rmt->fp_addr.ip4.as_u32;
240 match->lcl_port = lcl_port;
241 match->rmt_port = rmt_port;
242 mask = (session_mask_or_match_4_t *) & rule->mask;
243 ip4_preflen_to_mask (lcl->fp_len, &mask->lcl_ip);
244 ip4_preflen_to_mask (rmt->fp_len, &mask->rmt_ip);
245 mask->lcl_port = lcl_port == 0 ? 0 : (u16) ~ 0;
246 mask->rmt_port = rmt_port == 0 ? 0 : (u16) ~ 0;
247 max_match = (session_mask_or_match_4_t *) & rule->max_match;
248 ip4_prefix_max_address_host_order (&rmt->fp_addr.ip4, rmt->fp_len,
249 &max_match->rmt_ip);
250 ip4_prefix_max_address_host_order (&lcl->fp_addr.ip4, lcl->fp_len,
251 &max_match->lcl_ip);
252 max_match->lcl_port = lcl_port == 0 ? (u16) ~ 0 : lcl_port;
253 max_match->rmt_port = rmt_port == 0 ? (u16) ~ 0 : rmt_port;
254}
255
256void
257session_rules_table_init_rule_40 (mma_rule_40_t * rule,
258 fib_prefix_t * lcl, u16 lcl_port,
259 fib_prefix_t * rmt, u16 rmt_port)
260{
261 session_mask_or_match_6_t *match, *mask, *max_match;
262 fib_pref_normalize (lcl);
263 fib_pref_normalize (rmt);
264 match = (session_mask_or_match_6_t *) & rule->match;
265 clib_memcpy (&match->lcl_ip, &lcl->fp_addr.ip6, sizeof (match->lcl_ip));
266 clib_memcpy (&match->rmt_ip, &rmt->fp_addr.ip6, sizeof (match->rmt_ip));
267 match->lcl_port = lcl_port;
268 match->rmt_port = rmt_port;
269 mask = (session_mask_or_match_6_t *) & rule->mask;
270 ip6_preflen_to_mask (lcl->fp_len, &mask->lcl_ip);
271 ip6_preflen_to_mask (rmt->fp_len, &mask->rmt_ip);
272 mask->lcl_port = lcl_port == 0 ? 0 : (u16) ~ 0;
273 mask->rmt_port = rmt_port == 0 ? 0 : (u16) ~ 0;
274 max_match = (session_mask_or_match_6_t *) & rule->max_match;
275 ip6_prefix_max_address_host_order (&rmt->fp_addr.ip6, rmt->fp_len,
276 &max_match->rmt_ip);
277 ip6_prefix_max_address_host_order (&lcl->fp_addr.ip6, lcl->fp_len,
278 &max_match->lcl_ip);
279 max_match->lcl_port = lcl_port == 0 ? (u16) ~ 0 : lcl_port;
280 max_match->rmt_port = rmt_port == 0 ? (u16) ~ 0 : rmt_port;
281}
282
283mma_rule_16_t *
284session_rules_table_alloc_rule_16 (mma_rules_table_16_t * srt,
285 fib_prefix_t * lcl, u16 lcl_port,
286 fib_prefix_t * rmt, u16 rmt_port)
287{
288 mma_rule_16_t *rule = 0;
289 rule = mma_rules_table_rule_alloc_16 (srt);
290 session_rules_table_init_rule_16 (rule, lcl, lcl_port, rmt, rmt_port);
291 return rule;
292}
293
294mma_rule_40_t *
295session_rules_table_alloc_rule_40 (mma_rules_table_40_t * srt,
296 fib_prefix_t * lcl, u16 lcl_port,
297 fib_prefix_t * rmt, u16 rmt_port)
298{
299 mma_rule_40_t *rule;
300 rule = mma_rules_table_rule_alloc_40 (srt);
301 session_rules_table_init_rule_40 (rule, lcl, lcl_port, rmt, rmt_port);
302 return rule;
303}
304
Florin Corasc97a7392017-11-05 23:07:07 -0800305/**
306 * Add/delete session rule
307 *
308 * @param srt table where rule should be added
309 * @param args rule arguments
310 *
311 * @return 0 if success, clib_error_t error otherwise
312 */
Florin Coras1c710452017-10-17 00:03:13 -0700313clib_error_t *
314session_rules_table_add_del (session_rules_table_t * srt,
315 session_rule_table_add_del_args_t * args)
316{
317 u8 fib_proto = args->rmt.fp_proto;
Florin Corasc97a7392017-11-05 23:07:07 -0800318 u32 ri_from_tag, ri;
319 int rv;
Florin Coras1c710452017-10-17 00:03:13 -0700320
Florin Corasc97a7392017-11-05 23:07:07 -0800321 ri_from_tag = session_rules_table_rule_for_tag (srt, args->tag);
322 if (args->is_add && ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX)
Florin Coras1c710452017-10-17 00:03:13 -0700323 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
Florin Corasc97a7392017-11-05 23:07:07 -0800324 "tag exists");
Florin Coras1c710452017-10-17 00:03:13 -0700325
326 if (fib_proto == FIB_PROTOCOL_IP4)
327 {
328 mma_rules_table_16_t *srt4;
Florin Corasc97a7392017-11-05 23:07:07 -0800329 srt4 = &srt->session_rules_tables_16;
Florin Coras1c710452017-10-17 00:03:13 -0700330 if (args->is_add)
331 {
Florin Corasc97a7392017-11-05 23:07:07 -0800332 mma_rule_16_t *rule4;
333 rule4 = session_rules_table_alloc_rule_16 (srt4, &args->lcl,
334 args->lcl_port,
335 &args->rmt,
336 args->rmt_port);
337 rule4->action_index = args->action_index;
338 rv = mma_rules_table_add_rule_16 (srt4, rule4);
339 if (!rv)
340 {
341 ri = mma_rules_table_rule_index_16 (srt4, rule4);
342 session_rules_table_add_del_tag (srt, args->tag, ri, 1, 1);
343 }
Florin Coras1c710452017-10-17 00:03:13 -0700344 }
345 else
346 {
Florin Corasc97a7392017-11-05 23:07:07 -0800347 mma_rule_16_t *rule;
348 if (ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX)
349 {
350 rule = mma_rules_table_get_rule_16 (srt4, ri_from_tag);
351 mma_rules_table_del_rule_16 (srt4, rule, srt4->root_index);
352 session_rules_table_add_del_tag (srt, args->tag, 0, 1, 0);
353 }
354 else
355 {
356 mma_rule_16_t _rule;
357 rule = &_rule;
358 memset (rule, 0, sizeof (*rule));
359 session_rules_table_init_rule_16 (rule, &args->lcl,
360 args->lcl_port, &args->rmt,
361 args->rmt_port);
362 mma_rules_table_del_rule_16 (srt4, rule, srt4->root_index);
363 }
Florin Coras1c710452017-10-17 00:03:13 -0700364 }
365 }
366 else if (fib_proto == FIB_PROTOCOL_IP6)
367 {
368 mma_rules_table_40_t *srt6;
Florin Corasc97a7392017-11-05 23:07:07 -0800369 mma_rule_40_t *rule6;
370 srt6 = &srt->session_rules_tables_40;
Florin Coras1c710452017-10-17 00:03:13 -0700371 if (args->is_add)
372 {
Florin Corasc97a7392017-11-05 23:07:07 -0800373 rule6 = session_rules_table_alloc_rule_40 (srt6, &args->lcl,
374 args->lcl_port,
375 &args->rmt,
376 args->rmt_port);
377 rule6->action_index = args->action_index;
378 rv = mma_rules_table_add_rule_40 (srt6, rule6);
379 if (!rv)
380 {
381 ri = mma_rules_table_rule_index_40 (srt6, rule6);
382 session_rules_table_add_del_tag (srt, args->tag, ri, 0, 1);
383 }
Florin Coras1c710452017-10-17 00:03:13 -0700384 }
385 else
386 {
Florin Corasc97a7392017-11-05 23:07:07 -0800387 mma_rule_40_t *rule;
388 if (ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX)
389 {
390 rule = mma_rules_table_get_rule_40 (srt6, ri_from_tag);
391 mma_rules_table_del_rule_40 (srt6, rule, srt6->root_index);
392 session_rules_table_add_del_tag (srt, args->tag, 0, 0, 0);
393 }
394 else
395 {
396 mma_rule_40_t _rule;
397 rule = &_rule;
398 memset (rule, 0, sizeof (*rule));
399 session_rules_table_init_rule_40 (rule, &args->lcl,
400 args->lcl_port, &args->rmt,
401 args->rmt_port);
402 mma_rules_table_del_rule_40 (srt6, rule, srt6->root_index);
403 }
Florin Coras1c710452017-10-17 00:03:13 -0700404 }
405 }
406 else
407 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE_2, 0,
408 "invalid fib proto");
409 return 0;
410}
411
412u32
Florin Corasc97a7392017-11-05 23:07:07 -0800413session_rules_table_lookup4 (session_rules_table_t * srt,
Florin Coras1c710452017-10-17 00:03:13 -0700414 ip4_address_t * lcl_ip, ip4_address_t * rmt_ip,
415 u16 lcl_port, u16 rmt_port)
416{
Florin Corasc97a7392017-11-05 23:07:07 -0800417 mma_rules_table_16_t *srt4 = &srt->session_rules_tables_16;
Florin Coras1c710452017-10-17 00:03:13 -0700418 session_mask_or_match_4_t key = {
419 .lcl_ip.as_u32 = lcl_ip->as_u32,
420 .rmt_ip.as_u32 = rmt_ip->as_u32,
421 .lcl_port = lcl_port,
422 .rmt_port = rmt_port,
423 };
Florin Corasc97a7392017-11-05 23:07:07 -0800424 return mma_rules_table_lookup_16 (srt4, (mma_mask_or_match_16_t *) & key,
Florin Coras1c710452017-10-17 00:03:13 -0700425 srt4->root_index);
426}
427
428u32
Florin Corasc97a7392017-11-05 23:07:07 -0800429session_rules_table_lookup6 (session_rules_table_t * srt,
Florin Coras1c710452017-10-17 00:03:13 -0700430 ip6_address_t * lcl_ip, ip6_address_t * rmt_ip,
431 u16 lcl_port, u16 rmt_port)
432{
Florin Corasc97a7392017-11-05 23:07:07 -0800433 mma_rules_table_40_t *srt6 = &srt->session_rules_tables_40;
Florin Coras1c710452017-10-17 00:03:13 -0700434 session_mask_or_match_6_t key = {
435 .lcl_port = lcl_port,
436 .rmt_port = rmt_port,
437 };
Florin Corasa2e244c2017-10-29 10:56:15 -0700438 clib_memcpy (&key.lcl_ip, lcl_ip, sizeof (*lcl_ip));
439 clib_memcpy (&key.rmt_ip, rmt_ip, sizeof (*rmt_ip));
Florin Corasc97a7392017-11-05 23:07:07 -0800440 return mma_rules_table_lookup_40 (srt6, (mma_mask_or_match_40_t *) & key,
Florin Coras1c710452017-10-17 00:03:13 -0700441 srt6->root_index);
442}
443
444void
445session_rules_table_init (session_rules_table_t * srt)
446{
447 mma_rules_table_16_t *srt4;
448 mma_rules_table_40_t *srt6;
449 mma_rule_16_t *rule4;
450 mma_rule_40_t *rule6;
451 fib_prefix_t null_prefix;
Florin Coras1c710452017-10-17 00:03:13 -0700452
453 memset (&null_prefix, 0, sizeof (null_prefix));
454
Florin Corasc97a7392017-11-05 23:07:07 -0800455 srt4 = &srt->session_rules_tables_16;
456 rule4 = session_rules_table_alloc_rule_16 (srt4, &null_prefix, 0,
457 &null_prefix, 0);
458 rule4->action_index = SESSION_RULES_TABLE_INVALID_INDEX;
459 srt4->root_index = mma_rules_table_rule_index_16 (srt4, rule4);
460 srt4->rule_cmp_fn = rule_cmp_16;
Florin Coras1c710452017-10-17 00:03:13 -0700461
Florin Corasc97a7392017-11-05 23:07:07 -0800462 srt6 = &srt->session_rules_tables_40;
463 rule6 = session_rules_table_alloc_rule_40 (srt6, &null_prefix, 0,
464 &null_prefix, 0);
465 rule6->action_index = SESSION_RULES_TABLE_INVALID_INDEX;
466 srt6->root_index = mma_rules_table_rule_index_40 (srt6, rule6);
467 srt6->rule_cmp_fn = rule_cmp_40;
468
469 srt->rules_by_tag = hash_create_vec (0, sizeof (u8), sizeof (uword));
470 srt->tags_by_rules = hash_create (0, sizeof (uword));
Florin Coras1c710452017-10-17 00:03:13 -0700471}
472
473void
474session_rules_table_show_rule (vlib_main_t * vm, session_rules_table_t * srt,
Florin Corasc97a7392017-11-05 23:07:07 -0800475 ip46_address_t * lcl_ip, u16 lcl_port,
476 ip46_address_t * rmt_ip, u16 rmt_port,
477 u8 is_ip4)
Florin Coras1c710452017-10-17 00:03:13 -0700478{
479 mma_rules_table_16_t *srt4;
480 mma_rules_table_40_t *srt6;
481 mma_rule_16_t *sr4;
482 mma_rule_40_t *sr6;
483 u32 ri;
484
485 if (is_ip4)
486 {
Florin Corasc97a7392017-11-05 23:07:07 -0800487 srt4 = session_rules_table_get (srt, FIB_PROTOCOL_IP4);
Florin Coras1c710452017-10-17 00:03:13 -0700488 session_mask_or_match_4_t key = {
489 .lcl_ip.as_u32 = lcl_ip->ip4.as_u32,
490 .rmt_ip.as_u32 = rmt_ip->ip4.as_u32,
491 .lcl_port = lcl_port,
492 .rmt_port = rmt_port,
493 };
494 ri =
495 mma_rules_table_lookup_rule_16 (srt4,
496 (mma_mask_or_match_16_t *) & key,
497 srt4->root_index);
498 sr4 = mma_rules_table_get_rule_16 (srt4, ri);
Florin Corasc97a7392017-11-05 23:07:07 -0800499 vlib_cli_output (vm, "%U", format_session_rule4, srt, sr4);
Florin Coras1c710452017-10-17 00:03:13 -0700500 }
501 else
502 {
Florin Corasc97a7392017-11-05 23:07:07 -0800503 srt6 = session_rules_table_get (srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -0700504 session_mask_or_match_6_t key = {
505 .lcl_port = lcl_port,
506 .rmt_port = rmt_port,
507 };
Florin Corasa2e244c2017-10-29 10:56:15 -0700508 clib_memcpy (&key.lcl_ip, &lcl_ip->ip6, sizeof (lcl_ip->ip6));
509 clib_memcpy (&key.rmt_ip, &rmt_ip->ip6, sizeof (rmt_ip->ip6));
Florin Corasc97a7392017-11-05 23:07:07 -0800510 ri = mma_rules_table_lookup_rule_40 (srt6,
511 (mma_mask_or_match_40_t *) & key,
512 srt6->root_index);
Florin Coras1c710452017-10-17 00:03:13 -0700513 sr6 = mma_rules_table_get_rule_40 (srt6, ri);
Florin Corasc97a7392017-11-05 23:07:07 -0800514 vlib_cli_output (vm, "%U", format_session_rule6, srt, sr6);
Florin Coras1c710452017-10-17 00:03:13 -0700515 }
516}
517
518void
519session_rules_table_cli_dump (vlib_main_t * vm, session_rules_table_t * srt,
Florin Corasc97a7392017-11-05 23:07:07 -0800520 u8 fib_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700521{
522 if (fib_proto == FIB_PROTOCOL_IP4)
523 {
524 mma_rules_table_16_t *srt4;
525 mma_rule_16_t *sr4;
Florin Corasc97a7392017-11-05 23:07:07 -0800526 srt4 = &srt->session_rules_tables_16;
527 vlib_cli_output (vm, "IP4 rules");
Florin Coras1c710452017-10-17 00:03:13 -0700528
529 /* *INDENT-OFF* */
530 pool_foreach(sr4, srt4->rules, ({
Florin Corasc97a7392017-11-05 23:07:07 -0800531 vlib_cli_output (vm, "%U", format_session_rule4, srt, sr4);
Florin Coras1c710452017-10-17 00:03:13 -0700532 }));
533 /* *INDENT-ON* */
534
535 }
536 else if (fib_proto == FIB_PROTOCOL_IP6)
537 {
538 mma_rules_table_40_t *srt6;
539 mma_rule_40_t *sr6;
Florin Corasc97a7392017-11-05 23:07:07 -0800540 srt6 = &srt->session_rules_tables_40;
541 vlib_cli_output (vm, "IP6 rules");
Florin Coras1c710452017-10-17 00:03:13 -0700542
543 /* *INDENT-OFF* */
544 pool_foreach(sr6, srt6->rules, ({
Florin Corasc97a7392017-11-05 23:07:07 -0800545 vlib_cli_output (vm, "%U", format_session_rule6, srt, sr6);
Florin Coras1c710452017-10-17 00:03:13 -0700546 }));
547 /* *INDENT-ON* */
548
549 }
550}
551
552/*
553 * fd.io coding-style-patch-verification: ON
554 *
555 * Local Variables:
556 * eval: (c-set-style "gnu")
557 * End:
558 */