Line data Source code
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 368 : cnat_timestamp_new (f64 t) 24 : { 25 : u32 index; 26 : cnat_timestamp_t *ts; 27 368 : clib_rwlock_writer_lock (&cnat_main.ts_lock); 28 368 : pool_get (cnat_timestamps, ts); 29 368 : ts->last_seen = t; 30 368 : ts->lifetime = cnat_main.session_max_age; 31 368 : ts->refcnt = CNAT_TIMESTAMP_INIT_REFCNT; 32 368 : index = ts - cnat_timestamps; 33 368 : clib_rwlock_writer_unlock (&cnat_main.ts_lock); 34 368 : 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 3862 : cnat_timestamp_update (u32 index, f64 t) 48 : { 49 3862 : clib_rwlock_reader_lock (&cnat_main.ts_lock); 50 3862 : cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index); 51 3862 : ts->last_seen = t; 52 3862 : clib_rwlock_reader_unlock (&cnat_main.ts_lock); 53 3862 : } 54 : 55 : always_inline void 56 0 : cnat_timestamp_set_lifetime (u32 index, u16 lifetime) 57 : { 58 0 : clib_rwlock_reader_lock (&cnat_main.ts_lock); 59 0 : cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index); 60 0 : ts->lifetime = lifetime; 61 0 : clib_rwlock_reader_unlock (&cnat_main.ts_lock); 62 0 : } 63 : 64 : always_inline f64 65 4800 : cnat_timestamp_exp (u32 index) 66 : { 67 : f64 t; 68 4800 : if (INDEX_INVALID == index) 69 0 : return -1; 70 4800 : clib_rwlock_reader_lock (&cnat_main.ts_lock); 71 4800 : cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index); 72 4800 : t = ts->last_seen + (f64) ts->lifetime; 73 4800 : clib_rwlock_reader_unlock (&cnat_main.ts_lock); 74 4800 : return t; 75 : } 76 : 77 : always_inline void 78 196 : cnat_timestamp_free (u32 index) 79 : { 80 196 : if (INDEX_INVALID == index) 81 0 : return; 82 196 : clib_rwlock_writer_lock (&cnat_main.ts_lock); 83 196 : cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index); 84 196 : ts->refcnt--; 85 196 : if (0 == ts->refcnt) 86 98 : pool_put (cnat_timestamps, ts); 87 196 : 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