cnat: Introduce parametric source policy
[vpp.git] / src / plugins / cnat / cnat_inline.h
1 /*
2  * Copyright (c) 2020 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
17 #ifndef __CNAT_INLINE_H__
18 #define __CNAT_INLINE_H__
19
20 #include <cnat/cnat_types.h>
21
22 always_inline u32
23 cnat_timestamp_new (f64 t)
24 {
25   u32 index;
26   cnat_timestamp_t *ts;
27   clib_rwlock_writer_lock (&cnat_main.ts_lock);
28   pool_get (cnat_timestamps, ts);
29   ts->last_seen = t;
30   ts->lifetime = cnat_main.session_max_age;
31   ts->refcnt = CNAT_TIMESTAMP_INIT_REFCNT;
32   index = ts - cnat_timestamps;
33   clib_rwlock_writer_unlock (&cnat_main.ts_lock);
34   return index;
35 }
36
37 always_inline void
38 cnat_timestamp_inc_refcnt (u32 index)
39 {
40   clib_rwlock_reader_lock (&cnat_main.ts_lock);
41   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
42   ts->refcnt++;
43   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
44 }
45
46 always_inline void
47 cnat_timestamp_update (u32 index, f64 t)
48 {
49   clib_rwlock_reader_lock (&cnat_main.ts_lock);
50   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
51   ts->last_seen = t;
52   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
53 }
54
55 always_inline void
56 cnat_timestamp_set_lifetime (u32 index, u16 lifetime)
57 {
58   clib_rwlock_reader_lock (&cnat_main.ts_lock);
59   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
60   ts->lifetime = lifetime;
61   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
62 }
63
64 always_inline f64
65 cnat_timestamp_exp (u32 index)
66 {
67   f64 t;
68   if (INDEX_INVALID == index)
69     return -1;
70   clib_rwlock_reader_lock (&cnat_main.ts_lock);
71   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
72   t = ts->last_seen + (f64) ts->lifetime;
73   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
74   return t;
75 }
76
77 always_inline void
78 cnat_timestamp_free (u32 index)
79 {
80   if (INDEX_INVALID == index)
81     return;
82   clib_rwlock_writer_lock (&cnat_main.ts_lock);
83   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
84   ts->refcnt--;
85   if (0 == ts->refcnt)
86     pool_put (cnat_timestamps, ts);
87   clib_rwlock_writer_unlock (&cnat_main.ts_lock);
88 }
89
90 /*
91  * fd.io coding-style-patch-verification: ON
92  *
93  * Local Variables:
94  * eval: (c-set-style "gnu")
95  * End:
96  */
97
98 #endif