b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #ifndef FP_DATABASE_H |
| 2 | #define FP_DATABASE_H |
| 3 | |
| 4 | /* |
| 5 | * Fast path data base |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | /**-------------------------------------- |
| 14 | * Enum |
| 15 | *--------------------------------------*/ |
| 16 | |
| 17 | |
| 18 | static const char *const entry_state_names[] = { |
| 19 | "initialized", |
| 20 | "alive", |
| 21 | "dying", |
| 22 | }; |
| 23 | |
| 24 | enum entry_state { |
| 25 | ENTRY_INITIALIZED, |
| 26 | ENTRY_ALIVE, |
| 27 | ENTRY_DYING, |
| 28 | }; |
| 29 | |
| 30 | /**-------------------------------------- |
| 31 | * STRUCTURES |
| 32 | *--------------------------------------*/ |
| 33 | |
| 34 | /* Trace buffer per entry (enabled via DBG_LVL) */ |
| 35 | struct fpdb_trace { |
| 36 | struct list_head list; |
| 37 | int sz; |
| 38 | unsigned int hit_counter; |
| 39 | unsigned int timeout; |
| 40 | struct tcphdr tcph; |
| 41 | unsigned int tcp_state; |
| 42 | unsigned long ct_status; /* have we seen direction in both ways? */ |
| 43 | }; |
| 44 | |
| 45 | struct fpdb_debug { |
| 46 | unsigned int in_route_type; |
| 47 | unsigned int out_route_type; |
| 48 | struct fpdb_trace trace; |
| 49 | }; |
| 50 | |
| 51 | struct fpdb_entry { |
| 52 | /* Managment fields - DO NOT USE!*/ |
| 53 | struct hlist_node hlist; |
| 54 | struct rcu_head rcu; |
| 55 | atomic_t rc; |
| 56 | spinlock_t lock; |
| 57 | struct timer_list *guard_timer; |
| 58 | struct work_struct work; |
| 59 | |
| 60 | /* Entry data*/ |
| 61 | unsigned long tstamp; /* data base aging out use this field */ |
| 62 | struct nf_conn *ct; |
| 63 | enum ip_conntrack_dir dir; |
| 64 | unsigned long flags; |
| 65 | struct hh_cache hh; |
| 66 | struct nf_conntrack_tuple in_tuple; |
| 67 | struct nf_conntrack_tuple out_tuple; |
| 68 | struct fp_net_device *out_dev; /* destination interface handle */ |
| 69 | struct fp_net_device *in_dev; /* source interface handle */ |
| 70 | enum entry_state state; |
| 71 | bool block; |
| 72 | |
| 73 | /* statistics & debug */ |
| 74 | unsigned int bucket; |
| 75 | unsigned int hit_counter; |
| 76 | struct fpdb_debug debug; |
| 77 | #ifdef CONFIG_ASR_TOE |
| 78 | unsigned int nl_flag; |
| 79 | unsigned long detect_speed_jiffies; |
| 80 | unsigned int detect_speed_bytes; |
| 81 | unsigned int speed; /* Kbps */ |
| 82 | #endif |
| 83 | }; |
| 84 | |
| 85 | struct nf_conn_fastpath { |
| 86 | struct fpdb_entry *fpd_el[IP_CT_DIR_MAX]; |
| 87 | }; |
| 88 | |
| 89 | static inline struct nf_conn_fastpath *nfct_fastpath(const struct nf_conn *ct) |
| 90 | { |
| 91 | return nf_ct_ext_find(ct, NF_CT_EXT_FASTPATH); |
| 92 | } |
| 93 | |
| 94 | struct hist_entry { |
| 95 | unsigned int buckets; |
| 96 | unsigned int entries; |
| 97 | }; |
| 98 | |
| 99 | #define HISTOGRAM_SIZE (16) |
| 100 | |
| 101 | struct fpdb_stats { |
| 102 | /* HASH statistics */ |
| 103 | u32 size; /* number of buckets */ |
| 104 | u32 lookups; /* total lookups */ |
| 105 | u32 iterations; /* iterations per lookups */ |
| 106 | u32 hits; /* successfull lookups */ |
| 107 | u32 largest_bucket; /* Number of entries in largest bucket */ |
| 108 | u32 num_occupied; /* Number of occupied buckets */ |
| 109 | u32 load_factor; /* num of hashed entries / num of buckets */ |
| 110 | |
| 111 | /* HISTOGRAM */ |
| 112 | struct hist_entry hist[HISTOGRAM_SIZE + 1]; |
| 113 | |
| 114 | /* Database statistics */ |
| 115 | u32 avg_lookup_latency; |
| 116 | u32 max_lookup_latency; |
| 117 | u32 max_entries; /* max num of hashed entries */ |
| 118 | }; |
| 119 | |
| 120 | /**-------------------------------------- |
| 121 | * API FUNCTIONS |
| 122 | *--------------------------------------*/ |
| 123 | |
| 124 | void fpdb_lock_bh(void); |
| 125 | void fpdb_unlock_bh(void); |
| 126 | struct fpdb_entry *fpdb_alloc(gfp_t flags); |
| 127 | void fpdb_dump_entry(char *msg, struct fpdb_entry *el); |
| 128 | void fpdb_add(struct fpdb_entry *el); |
| 129 | void fpdb_replace(struct fpdb_entry *el, struct fpdb_entry *nel); |
| 130 | void fpdb_del(struct fpdb_entry *entry); |
| 131 | struct fpdb_entry *fpdb_get(struct nf_conntrack_tuple *tuple); |
| 132 | void fpdb_put(struct fpdb_entry *entry); |
| 133 | void fpdb_del_by_dev(struct net_device *dev); |
| 134 | void fpdb_del_by_port(unsigned int); |
| 135 | void fpdb_flush(void); |
| 136 | void fpdb_del_least_used_entry(int max_num); |
| 137 | void fpdb_trace(struct fpdb_entry *entry, struct tcphdr *tcph); |
| 138 | void fpdb_iterate(int (*iter)(struct fpdb_entry *e, void *data), void *data); |
| 139 | void fpdb_free(struct fpdb_entry * el); |
| 140 | int fpdb_del_block_entry_by_dev(struct fpdb_entry *el, void *data); |
| 141 | |
| 142 | #ifdef FP_USE_SRAM_POOL_OPT |
| 143 | extern unsigned long sram_pool_alloc(size_t size); |
| 144 | extern void sram_pool_free(unsigned long addr, size_t size); |
| 145 | #endif |
| 146 | |
| 147 | #endif /* FP_DATABASE_H */ |