blob: dee445dd0ad655d73d84d2f0864c1cb5313b30e1 [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
Florin Coras73e4f792017-11-22 19:22:48 -080053session_rules_table_del_tag (session_rules_table_t * srt, u8 * tag, u8 is_ip4)
Florin Corasc97a7392017-11-05 23:07:07 -080054{
55 uword *rip, *rtip;
56 session_rule_tag_t *rt;
57 u32 rti_key;
58
59 if (tag == 0)
60 return;
Florin Coras73e4f792017-11-22 19:22:48 -080061 rip = hash_get_mem (srt->rules_by_tag, tag);
62 if (!rip)
Florin Corasc97a7392017-11-05 23:07:07 -080063 {
Florin Coras73e4f792017-11-22 19:22:48 -080064 clib_warning ("tag has no rule associated");
65 return;
Florin Corasc97a7392017-11-05 23:07:07 -080066 }
Florin Coras73e4f792017-11-22 19:22:48 -080067 rti_key = session_rule_tag_key_index (*rip, is_ip4);
68 rtip = hash_get (srt->tags_by_rules, rti_key);
69 if (!rtip)
Florin Corasc97a7392017-11-05 23:07:07 -080070 {
Florin Coras73e4f792017-11-22 19:22:48 -080071 clib_warning ("rule has no tag associated");
72 return;
Florin Corasc97a7392017-11-05 23:07:07 -080073 }
Florin Coras73e4f792017-11-22 19:22:48 -080074 rt = pool_elt_at_index (srt->rule_tags, *rtip);
75 ASSERT (rt);
76 hash_unset_mem (srt->rules_by_tag, tag);
77 hash_unset (srt->tags_by_rules, rti_key);
78 pool_put (srt->rule_tags, rt);
79}
80
81void
82session_rules_table_add_tag (session_rules_table_t * srt, u8 * tag,
83 u32 rule_index, u8 is_ip4)
84{
85 uword *rip;
86 session_rule_tag_t *rt;
87 u32 rti_key;
88
89 if (tag == 0)
90 return;
91 rip = hash_get_mem (srt->rules_by_tag, tag);
92 if (rip)
93 session_rules_table_del_tag (srt, tag, is_ip4);
94 pool_get (srt->rule_tags, rt);
95 rt->tag = vec_dup (tag);
96 hash_set_mem (srt->rules_by_tag, rt->tag, rule_index);
97 rti_key = session_rule_tag_key_index (rule_index, is_ip4);
98 hash_set (srt->tags_by_rules, rti_key, rt - srt->rule_tags);
Florin Corasc97a7392017-11-05 23:07:07 -080099}
100
101u32
102session_rules_table_rule_for_tag (session_rules_table_t * srt, u8 * tag)
103{
104 uword *rp;
105 if (tag == 0)
106 return SESSION_RULES_TABLE_INVALID_INDEX;
107 rp = hash_get_mem (srt->rules_by_tag, tag);
108 return (rp == 0 ? SESSION_RULES_TABLE_INVALID_INDEX : *rp);
109}
110
Florin Coras1c710452017-10-17 00:03:13 -0700111static void
112fib_pref_normalize (fib_prefix_t * pref)
113{
114 if (pref->fp_proto == FIB_PROTOCOL_IP4)
115 ip4_address_normalize (&pref->fp_addr.ip4, pref->fp_len);
116 else
117 ip6_address_normalize (&pref->fp_addr.ip6, pref->fp_len);
118}
119
120u8 *
121format_session_rule4 (u8 * s, va_list * args)
122{
Florin Corasc97a7392017-11-05 23:07:07 -0800123 session_rules_table_t *srt = va_arg (*args, session_rules_table_t *);
Florin Coras1c710452017-10-17 00:03:13 -0700124 mma_rule_16_t *sr = va_arg (*args, mma_rule_16_t *);
125 session_mask_or_match_4_t *mask, *match;
Florin Corasc97a7392017-11-05 23:07:07 -0800126 mma_rules_table_16_t *srt4;
127 u8 *tag = 0, *null_tag = format (0, "none");
128 u32 ri;
Florin Coras1c710452017-10-17 00:03:13 -0700129 int i;
130
Florin Corasc97a7392017-11-05 23:07:07 -0800131 srt4 = &srt->session_rules_tables_16;
132 ri = mma_rules_table_rule_index_16 (srt4, sr);
133 tag = session_rules_table_rule_tag (srt, ri, 1);
Florin Coras1c710452017-10-17 00:03:13 -0700134 match = (session_mask_or_match_4_t *) & sr->match;
135 mask = (session_mask_or_match_4_t *) & sr->mask;
136
Florin Corasc97a7392017-11-05 23:07:07 -0800137 s = format (s, "[%d] rule: %U/%d %d %U/%d %d action: %d tag: %v", ri,
138 format_ip4_address, &match->lcl_ip,
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100139 ip4_mask_to_preflen (&mask->lcl_ip),
140 clib_net_to_host_u16 (match->lcl_port), format_ip4_address,
141 &match->rmt_ip, ip4_mask_to_preflen (&mask->rmt_ip),
142 clib_net_to_host_u16 (match->rmt_port), sr->action_index,
143 tag ? tag : null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700144 if (vec_len (sr->next_indices))
145 {
146 s = format (s, "\n children: ");
147 for (i = 0; i < vec_len (sr->next_indices); i++)
148 s = format (s, "%d ", sr->next_indices[i]);
149 }
Florin Corasc97a7392017-11-05 23:07:07 -0800150 vec_free (null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700151 return s;
152}
153
154u8 *
155format_session_rule6 (u8 * s, va_list * args)
156{
Florin Corasc97a7392017-11-05 23:07:07 -0800157 session_rules_table_t *srt = va_arg (*args, session_rules_table_t *);
Florin Coras1c710452017-10-17 00:03:13 -0700158 mma_rule_40_t *sr = va_arg (*args, mma_rule_40_t *);
159 session_mask_or_match_6_t *mask, *match;
Florin Corasc97a7392017-11-05 23:07:07 -0800160 mma_rules_table_40_t *srt6;
161 u8 *tag = 0, *null_tag = format (0, "none");
162 u32 ri;
Florin Coras1c710452017-10-17 00:03:13 -0700163 int i;
164
Florin Corasc97a7392017-11-05 23:07:07 -0800165 srt6 = &srt->session_rules_tables_40;
166 ri = mma_rules_table_rule_index_40 (srt6, sr);
167 tag = session_rules_table_rule_tag (srt, ri, 0);
Florin Coras1c710452017-10-17 00:03:13 -0700168 match = (session_mask_or_match_6_t *) & sr->match;
169 mask = (session_mask_or_match_6_t *) & sr->mask;
170
Florin Corasc97a7392017-11-05 23:07:07 -0800171 s = format (s, "[%d] rule: %U/%d %d %U/%d %d action: %d tag: %v", ri,
172 format_ip6_address, &match->lcl_ip,
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100173 ip6_mask_to_preflen (&mask->lcl_ip),
174 clib_net_to_host_u16 (match->lcl_port), format_ip6_address,
175 &match->rmt_ip, ip6_mask_to_preflen (&mask->rmt_ip),
176 clib_net_to_host_u16 (match->rmt_port), sr->action_index,
177 tag ? tag : null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700178 if (vec_len (sr->next_indices))
179 {
180 s = format (s, "\n children: ");
181 for (i = 0; i < vec_len (sr->next_indices); i++)
182 s = format (s, "%d ", sr->next_indices[i]);
183 }
Florin Corasc97a7392017-11-05 23:07:07 -0800184 vec_free (null_tag);
Florin Coras1c710452017-10-17 00:03:13 -0700185 return s;
186}
187
188void *
Florin Corasc97a7392017-11-05 23:07:07 -0800189session_rules_table_get (session_rules_table_t * srt, u8 fib_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700190{
191 if (fib_proto == FIB_PROTOCOL_IP4)
Florin Corasc97a7392017-11-05 23:07:07 -0800192 return &srt->session_rules_tables_16;
Florin Coras1c710452017-10-17 00:03:13 -0700193 else if (fib_proto == FIB_PROTOCOL_IP6)
Florin Corasc97a7392017-11-05 23:07:07 -0800194 return &srt->session_rules_tables_40;
Florin Coras1c710452017-10-17 00:03:13 -0700195 return 0;
196}
197
198int
199rule_cmp_16 (mma_rule_16_t * rule1, mma_rule_16_t * rule2)
200{
201 session_mask_or_match_4_t *m1, *m2;
202
203 m1 = (session_mask_or_match_4_t *) & rule1->max_match;
204 m2 = (session_mask_or_match_4_t *) & rule2->max_match;
205 if (m1->rmt_ip.as_u32 != m2->rmt_ip.as_u32)
206 return (m1->rmt_ip.as_u32 < m2->rmt_ip.as_u32 ? -1 : 1);
207 if (m1->lcl_ip.as_u32 != m2->lcl_ip.as_u32)
208 return (m1->lcl_ip.as_u32 < m2->lcl_ip.as_u32 ? -1 : 1);
209 if (m1->rmt_port != m2->rmt_port)
210 return (m1->rmt_port < m2->rmt_port ? -1 : 1);
211 if (m1->lcl_port != m2->lcl_port)
212 return (m1->lcl_port < m2->lcl_port ? -1 : 1);
213 return 0;
214}
215
216int
217rule_cmp_40 (mma_rule_40_t * rule1, mma_rule_40_t * rule2)
218{
219 session_mask_or_match_6_t *r1, *r2;
220 r1 = (session_mask_or_match_6_t *) & rule1->max_match;
221 r2 = (session_mask_or_match_6_t *) & rule2->max_match;
222 if (r1->rmt_ip.as_u64[0] != r2->rmt_ip.as_u64[0])
223 return (r1->rmt_ip.as_u64[0] < r2->rmt_ip.as_u64[0] ? -1 : 1);
224 if (r1->rmt_ip.as_u64[1] != r2->rmt_ip.as_u64[1])
225 return (r1->rmt_ip.as_u64[1] < r2->rmt_ip.as_u64[1] ? -1 : 1);
226 if (r1->lcl_ip.as_u64[0] != r2->lcl_ip.as_u64[0])
227 return (r1->lcl_ip.as_u64[0] < r2->lcl_ip.as_u64[0] ? -1 : 1);
228 if (r1->lcl_ip.as_u64[1] != r2->lcl_ip.as_u64[1])
229 return (r1->lcl_ip.as_u64[1] < r2->lcl_ip.as_u64[1]) ? -1 : 1;
230 if (r1->rmt_port != r2->rmt_port)
231 return (r1->rmt_port < r2->rmt_port ? -1 : 1);
232 if (r1->lcl_port != r2->lcl_port)
233 return (r1->lcl_port < r2->lcl_port ? -1 : 1);
234 return 0;
235}
236
237void
238session_rules_table_init_rule_16 (mma_rule_16_t * rule,
239 fib_prefix_t * lcl, u16 lcl_port,
240 fib_prefix_t * rmt, u16 rmt_port)
241{
242 session_mask_or_match_4_t *match, *mask, *max_match;
243 fib_pref_normalize (lcl);
244 fib_pref_normalize (rmt);
245 match = (session_mask_or_match_4_t *) & rule->match;
246 match->lcl_ip.as_u32 = lcl->fp_addr.ip4.as_u32;
247 match->rmt_ip.as_u32 = rmt->fp_addr.ip4.as_u32;
248 match->lcl_port = lcl_port;
249 match->rmt_port = rmt_port;
250 mask = (session_mask_or_match_4_t *) & rule->mask;
251 ip4_preflen_to_mask (lcl->fp_len, &mask->lcl_ip);
252 ip4_preflen_to_mask (rmt->fp_len, &mask->rmt_ip);
253 mask->lcl_port = lcl_port == 0 ? 0 : (u16) ~ 0;
254 mask->rmt_port = rmt_port == 0 ? 0 : (u16) ~ 0;
255 max_match = (session_mask_or_match_4_t *) & rule->max_match;
256 ip4_prefix_max_address_host_order (&rmt->fp_addr.ip4, rmt->fp_len,
257 &max_match->rmt_ip);
258 ip4_prefix_max_address_host_order (&lcl->fp_addr.ip4, lcl->fp_len,
259 &max_match->lcl_ip);
260 max_match->lcl_port = lcl_port == 0 ? (u16) ~ 0 : lcl_port;
261 max_match->rmt_port = rmt_port == 0 ? (u16) ~ 0 : rmt_port;
262}
263
264void
265session_rules_table_init_rule_40 (mma_rule_40_t * rule,
266 fib_prefix_t * lcl, u16 lcl_port,
267 fib_prefix_t * rmt, u16 rmt_port)
268{
269 session_mask_or_match_6_t *match, *mask, *max_match;
270 fib_pref_normalize (lcl);
271 fib_pref_normalize (rmt);
272 match = (session_mask_or_match_6_t *) & rule->match;
273 clib_memcpy (&match->lcl_ip, &lcl->fp_addr.ip6, sizeof (match->lcl_ip));
274 clib_memcpy (&match->rmt_ip, &rmt->fp_addr.ip6, sizeof (match->rmt_ip));
275 match->lcl_port = lcl_port;
276 match->rmt_port = rmt_port;
277 mask = (session_mask_or_match_6_t *) & rule->mask;
278 ip6_preflen_to_mask (lcl->fp_len, &mask->lcl_ip);
279 ip6_preflen_to_mask (rmt->fp_len, &mask->rmt_ip);
280 mask->lcl_port = lcl_port == 0 ? 0 : (u16) ~ 0;
281 mask->rmt_port = rmt_port == 0 ? 0 : (u16) ~ 0;
282 max_match = (session_mask_or_match_6_t *) & rule->max_match;
283 ip6_prefix_max_address_host_order (&rmt->fp_addr.ip6, rmt->fp_len,
284 &max_match->rmt_ip);
285 ip6_prefix_max_address_host_order (&lcl->fp_addr.ip6, lcl->fp_len,
286 &max_match->lcl_ip);
287 max_match->lcl_port = lcl_port == 0 ? (u16) ~ 0 : lcl_port;
288 max_match->rmt_port = rmt_port == 0 ? (u16) ~ 0 : rmt_port;
289}
290
291mma_rule_16_t *
292session_rules_table_alloc_rule_16 (mma_rules_table_16_t * srt,
293 fib_prefix_t * lcl, u16 lcl_port,
294 fib_prefix_t * rmt, u16 rmt_port)
295{
296 mma_rule_16_t *rule = 0;
297 rule = mma_rules_table_rule_alloc_16 (srt);
298 session_rules_table_init_rule_16 (rule, lcl, lcl_port, rmt, rmt_port);
299 return rule;
300}
301
302mma_rule_40_t *
303session_rules_table_alloc_rule_40 (mma_rules_table_40_t * srt,
304 fib_prefix_t * lcl, u16 lcl_port,
305 fib_prefix_t * rmt, u16 rmt_port)
306{
307 mma_rule_40_t *rule;
308 rule = mma_rules_table_rule_alloc_40 (srt);
309 session_rules_table_init_rule_40 (rule, lcl, lcl_port, rmt, rmt_port);
310 return rule;
311}
312
Florin Coras73e4f792017-11-22 19:22:48 -0800313u32
314session_rules_table_lookup_rule4 (session_rules_table_t * srt,
315 ip4_address_t * lcl_ip,
316 ip4_address_t * rmt_ip, u16 lcl_port,
317 u16 rmt_port)
318{
319 mma_rules_table_16_t *srt4 = &srt->session_rules_tables_16;
320 session_mask_or_match_4_t key = {
321 .lcl_ip.as_u32 = lcl_ip->as_u32,
322 .rmt_ip.as_u32 = rmt_ip->as_u32,
323 .lcl_port = lcl_port,
324 .rmt_port = rmt_port,
325 };
326 return mma_rules_table_lookup_rule_16 (srt4,
327 (mma_mask_or_match_16_t *) & key,
328 srt4->root_index);
329}
330
331u32
332session_rules_table_lookup4 (session_rules_table_t * srt,
333 ip4_address_t * lcl_ip, ip4_address_t * rmt_ip,
334 u16 lcl_port, u16 rmt_port)
335{
336 mma_rules_table_16_t *srt4 = &srt->session_rules_tables_16;
337 session_mask_or_match_4_t key = {
338 .lcl_ip.as_u32 = lcl_ip->as_u32,
339 .rmt_ip.as_u32 = rmt_ip->as_u32,
340 .lcl_port = lcl_port,
341 .rmt_port = rmt_port,
342 };
343 return mma_rules_table_lookup_16 (srt4, (mma_mask_or_match_16_t *) & key,
344 srt4->root_index);
345}
346
347u32
348session_rules_table_lookup_rule6 (session_rules_table_t * srt,
349 ip6_address_t * lcl_ip,
350 ip6_address_t * rmt_ip, u16 lcl_port,
351 u16 rmt_port)
352{
353 mma_rules_table_40_t *srt6 = &srt->session_rules_tables_40;
354 session_mask_or_match_6_t key = {
355 .lcl_port = lcl_port,
356 .rmt_port = rmt_port,
357 };
358 clib_memcpy (&key.lcl_ip, lcl_ip, sizeof (*lcl_ip));
359 clib_memcpy (&key.rmt_ip, rmt_ip, sizeof (*rmt_ip));
360 return mma_rules_table_lookup_rule_40 (srt6,
361 (mma_mask_or_match_40_t *) & key,
362 srt6->root_index);
363}
364
365u32
366session_rules_table_lookup6 (session_rules_table_t * srt,
367 ip6_address_t * lcl_ip, ip6_address_t * rmt_ip,
368 u16 lcl_port, u16 rmt_port)
369{
370 mma_rules_table_40_t *srt6 = &srt->session_rules_tables_40;
371 session_mask_or_match_6_t key = {
372 .lcl_port = lcl_port,
373 .rmt_port = rmt_port,
374 };
375 clib_memcpy (&key.lcl_ip, lcl_ip, sizeof (*lcl_ip));
376 clib_memcpy (&key.rmt_ip, rmt_ip, sizeof (*rmt_ip));
377 return mma_rules_table_lookup_40 (srt6, (mma_mask_or_match_40_t *) & key,
378 srt6->root_index);
379}
380
Florin Corasc97a7392017-11-05 23:07:07 -0800381/**
382 * Add/delete session rule
383 *
384 * @param srt table where rule should be added
385 * @param args rule arguments
386 *
387 * @return 0 if success, clib_error_t error otherwise
388 */
Florin Coras1c710452017-10-17 00:03:13 -0700389clib_error_t *
390session_rules_table_add_del (session_rules_table_t * srt,
391 session_rule_table_add_del_args_t * args)
392{
Florin Coras73e4f792017-11-22 19:22:48 -0800393 u8 fib_proto = args->rmt.fp_proto, *rt;
Florin Corasc97a7392017-11-05 23:07:07 -0800394 u32 ri_from_tag, ri;
395 int rv;
Florin Coras1c710452017-10-17 00:03:13 -0700396
Florin Corasc97a7392017-11-05 23:07:07 -0800397 ri_from_tag = session_rules_table_rule_for_tag (srt, args->tag);
398 if (args->is_add && ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX)
Florin Coras1c710452017-10-17 00:03:13 -0700399 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
Florin Corasc97a7392017-11-05 23:07:07 -0800400 "tag exists");
Florin Coras1c710452017-10-17 00:03:13 -0700401
402 if (fib_proto == FIB_PROTOCOL_IP4)
403 {
404 mma_rules_table_16_t *srt4;
Florin Corasc97a7392017-11-05 23:07:07 -0800405 srt4 = &srt->session_rules_tables_16;
Florin Coras1c710452017-10-17 00:03:13 -0700406 if (args->is_add)
407 {
Florin Corasc97a7392017-11-05 23:07:07 -0800408 mma_rule_16_t *rule4;
409 rule4 = session_rules_table_alloc_rule_16 (srt4, &args->lcl,
410 args->lcl_port,
411 &args->rmt,
412 args->rmt_port);
413 rule4->action_index = args->action_index;
414 rv = mma_rules_table_add_rule_16 (srt4, rule4);
415 if (!rv)
416 {
417 ri = mma_rules_table_rule_index_16 (srt4, rule4);
Florin Coras73e4f792017-11-22 19:22:48 -0800418 session_rules_table_add_tag (srt, args->tag, ri, 1);
419 }
420 else
421 {
422 ri = session_rules_table_lookup_rule4 (srt,
423 &args->lcl.fp_addr.ip4,
424 &args->rmt.fp_addr.ip4,
425 args->lcl_port,
426 args->rmt_port);
427 if (ri != SESSION_RULES_TABLE_INVALID_INDEX)
428 {
429 rt = session_rules_table_rule_tag (srt, ri, 1);
430 session_rules_table_del_tag (srt, rt, 1);
431 session_rules_table_add_tag (srt, args->tag, ri, 1);
432 }
Florin Corasc97a7392017-11-05 23:07:07 -0800433 }
Florin Coras1c710452017-10-17 00:03:13 -0700434 }
435 else
436 {
Florin Corasc97a7392017-11-05 23:07:07 -0800437 mma_rule_16_t *rule;
438 if (ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX)
439 {
440 rule = mma_rules_table_get_rule_16 (srt4, ri_from_tag);
441 mma_rules_table_del_rule_16 (srt4, rule, srt4->root_index);
Florin Coras73e4f792017-11-22 19:22:48 -0800442 session_rules_table_del_tag (srt, args->tag, 1);
Florin Corasc97a7392017-11-05 23:07:07 -0800443 }
444 else
445 {
446 mma_rule_16_t _rule;
447 rule = &_rule;
448 memset (rule, 0, sizeof (*rule));
449 session_rules_table_init_rule_16 (rule, &args->lcl,
450 args->lcl_port, &args->rmt,
451 args->rmt_port);
452 mma_rules_table_del_rule_16 (srt4, rule, srt4->root_index);
453 }
Florin Coras1c710452017-10-17 00:03:13 -0700454 }
455 }
456 else if (fib_proto == FIB_PROTOCOL_IP6)
457 {
458 mma_rules_table_40_t *srt6;
Florin Corasc97a7392017-11-05 23:07:07 -0800459 mma_rule_40_t *rule6;
460 srt6 = &srt->session_rules_tables_40;
Florin Coras1c710452017-10-17 00:03:13 -0700461 if (args->is_add)
462 {
Florin Corasc97a7392017-11-05 23:07:07 -0800463 rule6 = session_rules_table_alloc_rule_40 (srt6, &args->lcl,
464 args->lcl_port,
465 &args->rmt,
466 args->rmt_port);
467 rule6->action_index = args->action_index;
468 rv = mma_rules_table_add_rule_40 (srt6, rule6);
469 if (!rv)
470 {
471 ri = mma_rules_table_rule_index_40 (srt6, rule6);
Florin Coras73e4f792017-11-22 19:22:48 -0800472 session_rules_table_add_tag (srt, args->tag, ri, 0);
473 }
474 else
475 {
476 ri = session_rules_table_lookup_rule6 (srt,
477 &args->lcl.fp_addr.ip6,
478 &args->rmt.fp_addr.ip6,
479 args->lcl_port,
480 args->rmt_port);
481 if (ri != SESSION_RULES_TABLE_INVALID_INDEX)
482 {
483 rt = session_rules_table_rule_tag (srt, ri, 0);
484 session_rules_table_del_tag (srt, rt, 1);
485 session_rules_table_add_tag (srt, args->tag, ri, 0);
486 }
Florin Corasc97a7392017-11-05 23:07:07 -0800487 }
Florin Coras1c710452017-10-17 00:03:13 -0700488 }
489 else
490 {
Florin Corasc97a7392017-11-05 23:07:07 -0800491 mma_rule_40_t *rule;
492 if (ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX)
493 {
494 rule = mma_rules_table_get_rule_40 (srt6, ri_from_tag);
495 mma_rules_table_del_rule_40 (srt6, rule, srt6->root_index);
Florin Coras73e4f792017-11-22 19:22:48 -0800496 session_rules_table_del_tag (srt, args->tag, 0);
Florin Corasc97a7392017-11-05 23:07:07 -0800497 }
498 else
499 {
500 mma_rule_40_t _rule;
501 rule = &_rule;
502 memset (rule, 0, sizeof (*rule));
503 session_rules_table_init_rule_40 (rule, &args->lcl,
504 args->lcl_port, &args->rmt,
505 args->rmt_port);
506 mma_rules_table_del_rule_40 (srt6, rule, srt6->root_index);
507 }
Florin Coras1c710452017-10-17 00:03:13 -0700508 }
509 }
510 else
511 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE_2, 0,
512 "invalid fib proto");
513 return 0;
514}
515
Florin Coras1c710452017-10-17 00:03:13 -0700516void
517session_rules_table_init (session_rules_table_t * srt)
518{
519 mma_rules_table_16_t *srt4;
520 mma_rules_table_40_t *srt6;
521 mma_rule_16_t *rule4;
522 mma_rule_40_t *rule6;
523 fib_prefix_t null_prefix;
Florin Coras1c710452017-10-17 00:03:13 -0700524
525 memset (&null_prefix, 0, sizeof (null_prefix));
526
Florin Corasc97a7392017-11-05 23:07:07 -0800527 srt4 = &srt->session_rules_tables_16;
528 rule4 = session_rules_table_alloc_rule_16 (srt4, &null_prefix, 0,
529 &null_prefix, 0);
530 rule4->action_index = SESSION_RULES_TABLE_INVALID_INDEX;
531 srt4->root_index = mma_rules_table_rule_index_16 (srt4, rule4);
532 srt4->rule_cmp_fn = rule_cmp_16;
Florin Coras1c710452017-10-17 00:03:13 -0700533
Florin Corasc97a7392017-11-05 23:07:07 -0800534 srt6 = &srt->session_rules_tables_40;
535 rule6 = session_rules_table_alloc_rule_40 (srt6, &null_prefix, 0,
536 &null_prefix, 0);
537 rule6->action_index = SESSION_RULES_TABLE_INVALID_INDEX;
538 srt6->root_index = mma_rules_table_rule_index_40 (srt6, rule6);
539 srt6->rule_cmp_fn = rule_cmp_40;
540
541 srt->rules_by_tag = hash_create_vec (0, sizeof (u8), sizeof (uword));
542 srt->tags_by_rules = hash_create (0, sizeof (uword));
Florin Coras1c710452017-10-17 00:03:13 -0700543}
544
545void
546session_rules_table_show_rule (vlib_main_t * vm, session_rules_table_t * srt,
Florin Corasc97a7392017-11-05 23:07:07 -0800547 ip46_address_t * lcl_ip, u16 lcl_port,
548 ip46_address_t * rmt_ip, u16 rmt_port,
549 u8 is_ip4)
Florin Coras1c710452017-10-17 00:03:13 -0700550{
551 mma_rules_table_16_t *srt4;
552 mma_rules_table_40_t *srt6;
553 mma_rule_16_t *sr4;
554 mma_rule_40_t *sr6;
555 u32 ri;
556
557 if (is_ip4)
558 {
Florin Corasc97a7392017-11-05 23:07:07 -0800559 srt4 = session_rules_table_get (srt, FIB_PROTOCOL_IP4);
Florin Coras1c710452017-10-17 00:03:13 -0700560 session_mask_or_match_4_t key = {
561 .lcl_ip.as_u32 = lcl_ip->ip4.as_u32,
562 .rmt_ip.as_u32 = rmt_ip->ip4.as_u32,
563 .lcl_port = lcl_port,
564 .rmt_port = rmt_port,
565 };
566 ri =
567 mma_rules_table_lookup_rule_16 (srt4,
568 (mma_mask_or_match_16_t *) & key,
569 srt4->root_index);
570 sr4 = mma_rules_table_get_rule_16 (srt4, ri);
Florin Corasc97a7392017-11-05 23:07:07 -0800571 vlib_cli_output (vm, "%U", format_session_rule4, srt, sr4);
Florin Coras1c710452017-10-17 00:03:13 -0700572 }
573 else
574 {
Florin Corasc97a7392017-11-05 23:07:07 -0800575 srt6 = session_rules_table_get (srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -0700576 session_mask_or_match_6_t key = {
577 .lcl_port = lcl_port,
578 .rmt_port = rmt_port,
579 };
Florin Corasa2e244c2017-10-29 10:56:15 -0700580 clib_memcpy (&key.lcl_ip, &lcl_ip->ip6, sizeof (lcl_ip->ip6));
581 clib_memcpy (&key.rmt_ip, &rmt_ip->ip6, sizeof (rmt_ip->ip6));
Florin Corasc97a7392017-11-05 23:07:07 -0800582 ri = mma_rules_table_lookup_rule_40 (srt6,
583 (mma_mask_or_match_40_t *) & key,
584 srt6->root_index);
Florin Coras1c710452017-10-17 00:03:13 -0700585 sr6 = mma_rules_table_get_rule_40 (srt6, ri);
Florin Corasc97a7392017-11-05 23:07:07 -0800586 vlib_cli_output (vm, "%U", format_session_rule6, srt, sr6);
Florin Coras1c710452017-10-17 00:03:13 -0700587 }
588}
589
590void
591session_rules_table_cli_dump (vlib_main_t * vm, session_rules_table_t * srt,
Florin Corasc97a7392017-11-05 23:07:07 -0800592 u8 fib_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700593{
594 if (fib_proto == FIB_PROTOCOL_IP4)
595 {
596 mma_rules_table_16_t *srt4;
597 mma_rule_16_t *sr4;
Florin Corasc97a7392017-11-05 23:07:07 -0800598 srt4 = &srt->session_rules_tables_16;
599 vlib_cli_output (vm, "IP4 rules");
Florin Coras1c710452017-10-17 00:03:13 -0700600
601 /* *INDENT-OFF* */
602 pool_foreach(sr4, srt4->rules, ({
Florin Corasc97a7392017-11-05 23:07:07 -0800603 vlib_cli_output (vm, "%U", format_session_rule4, srt, sr4);
Florin Coras1c710452017-10-17 00:03:13 -0700604 }));
605 /* *INDENT-ON* */
606
607 }
608 else if (fib_proto == FIB_PROTOCOL_IP6)
609 {
610 mma_rules_table_40_t *srt6;
611 mma_rule_40_t *sr6;
Florin Corasc97a7392017-11-05 23:07:07 -0800612 srt6 = &srt->session_rules_tables_40;
613 vlib_cli_output (vm, "IP6 rules");
Florin Coras1c710452017-10-17 00:03:13 -0700614
615 /* *INDENT-OFF* */
616 pool_foreach(sr6, srt6->rules, ({
Florin Corasc97a7392017-11-05 23:07:07 -0800617 vlib_cli_output (vm, "%U", format_session_rule6, srt, sr6);
Florin Coras1c710452017-10-17 00:03:13 -0700618 }));
619 /* *INDENT-ON* */
620
621 }
622}
623
624/*
625 * fd.io coding-style-patch-verification: ON
626 *
627 * Local Variables:
628 * eval: (c-set-style "gnu")
629 * End:
630 */