Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/types.h> |
| 2 | #include <linux/atmmpc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 3 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/time.h> |
| 5 | |
| 6 | #include "mpoa_caches.h" |
| 7 | #include "mpc.h" |
| 8 | |
| 9 | /* |
| 10 | * mpoa_caches.c: Implementation of ingress and egress cache |
| 11 | * handling functions |
| 12 | */ |
| 13 | |
| 14 | #if 0 |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 15 | #define dprintk(format, args...) \ |
| 16 | printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #else |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 18 | #define dprintk(format, args...) \ |
| 19 | do { if (0) \ |
| 20 | printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\ |
| 21 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | #if 0 |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 25 | #define ddprintk(format, args...) \ |
| 26 | printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #else |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 28 | #define ddprintk(format, args...) \ |
| 29 | do { if (0) \ |
| 30 | printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\ |
| 31 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame] | 34 | static in_cache_entry *in_cache_get(__be32 dst_ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct mpoa_client *client) |
| 36 | { |
| 37 | in_cache_entry *entry; |
| 38 | |
| 39 | read_lock_bh(&client->ingress_lock); |
| 40 | entry = client->in_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 41 | while (entry != NULL) { |
| 42 | if (entry->ctrl_info.in_dst_ip == dst_ip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | atomic_inc(&entry->use); |
| 44 | read_unlock_bh(&client->ingress_lock); |
| 45 | return entry; |
| 46 | } |
| 47 | entry = entry->next; |
| 48 | } |
| 49 | read_unlock_bh(&client->ingress_lock); |
| 50 | |
| 51 | return NULL; |
| 52 | } |
| 53 | |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame] | 54 | static in_cache_entry *in_cache_get_with_mask(__be32 dst_ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | struct mpoa_client *client, |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame] | 56 | __be32 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | in_cache_entry *entry; |
| 59 | |
| 60 | read_lock_bh(&client->ingress_lock); |
| 61 | entry = client->in_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 62 | while (entry != NULL) { |
| 63 | if ((entry->ctrl_info.in_dst_ip & mask) == (dst_ip & mask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | atomic_inc(&entry->use); |
| 65 | read_unlock_bh(&client->ingress_lock); |
| 66 | return entry; |
| 67 | } |
| 68 | entry = entry->next; |
| 69 | } |
| 70 | read_unlock_bh(&client->ingress_lock); |
| 71 | |
| 72 | return NULL; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | static in_cache_entry *in_cache_get_by_vcc(struct atm_vcc *vcc, |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 77 | struct mpoa_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | in_cache_entry *entry; |
| 80 | |
| 81 | read_lock_bh(&client->ingress_lock); |
| 82 | entry = client->in_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 83 | while (entry != NULL) { |
| 84 | if (entry->shortcut == vcc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | atomic_inc(&entry->use); |
| 86 | read_unlock_bh(&client->ingress_lock); |
| 87 | return entry; |
| 88 | } |
| 89 | entry = entry->next; |
| 90 | } |
| 91 | read_unlock_bh(&client->ingress_lock); |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame] | 96 | static in_cache_entry *in_cache_add_entry(__be32 dst_ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | struct mpoa_client *client) |
| 98 | { |
Arnaldo Carvalho de Melo | 2afe37c | 2006-11-21 01:14:33 -0200 | [diff] [blame] | 99 | in_cache_entry *entry = kzalloc(sizeof(in_cache_entry), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | if (entry == NULL) { |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 102 | pr_info("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
| 105 | |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 106 | dprintk("adding an ingress entry, ip = %pI4\n", &dst_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | atomic_set(&entry->use, 1); |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 109 | dprintk("new_in_cache_entry: about to lock\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | write_lock_bh(&client->ingress_lock); |
| 111 | entry->next = client->in_cache; |
| 112 | entry->prev = NULL; |
| 113 | if (client->in_cache != NULL) |
| 114 | client->in_cache->prev = entry; |
| 115 | client->in_cache = entry; |
| 116 | |
| 117 | memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN); |
| 118 | entry->ctrl_info.in_dst_ip = dst_ip; |
| 119 | do_gettimeofday(&(entry->tv)); |
| 120 | entry->retry_time = client->parameters.mpc_p4; |
| 121 | entry->count = 1; |
| 122 | entry->entry_state = INGRESS_INVALID; |
| 123 | entry->ctrl_info.holding_time = HOLDING_TIME_DEFAULT; |
| 124 | atomic_inc(&entry->use); |
| 125 | |
| 126 | write_unlock_bh(&client->ingress_lock); |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 127 | dprintk("new_in_cache_entry: unlocked\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | return entry; |
| 130 | } |
| 131 | |
| 132 | static int cache_hit(in_cache_entry *entry, struct mpoa_client *mpc) |
| 133 | { |
| 134 | struct atm_mpoa_qos *qos; |
| 135 | struct k_message msg; |
| 136 | |
| 137 | entry->count++; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 138 | if (entry->entry_state == INGRESS_RESOLVED && entry->shortcut != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return OPEN; |
| 140 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 141 | if (entry->entry_state == INGRESS_REFRESHING) { |
| 142 | if (entry->count > mpc->parameters.mpc_p1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | msg.type = SND_MPOA_RES_RQST; |
| 144 | msg.content.in_info = entry->ctrl_info; |
| 145 | memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN); |
| 146 | qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 147 | if (qos != NULL) |
| 148 | msg.qos = qos->qos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | msg_to_mpoad(&msg, mpc); |
| 150 | do_gettimeofday(&(entry->reply_wait)); |
| 151 | entry->entry_state = INGRESS_RESOLVING; |
| 152 | } |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 153 | if (entry->shortcut != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return OPEN; |
| 155 | return CLOSED; |
| 156 | } |
| 157 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 158 | if (entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return OPEN; |
| 160 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 161 | if (entry->count > mpc->parameters.mpc_p1 && |
| 162 | entry->entry_state == INGRESS_INVALID) { |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 163 | dprintk("(%s) threshold exceeded for ip %pI4, sending MPOA res req\n", |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 164 | mpc->dev->name, &entry->ctrl_info.in_dst_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | entry->entry_state = INGRESS_RESOLVING; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 166 | msg.type = SND_MPOA_RES_RQST; |
| 167 | memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | msg.content.in_info = entry->ctrl_info; |
| 169 | qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 170 | if (qos != NULL) |
| 171 | msg.qos = qos->qos; |
| 172 | msg_to_mpoad(&msg, mpc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | do_gettimeofday(&(entry->reply_wait)); |
| 174 | } |
| 175 | |
| 176 | return CLOSED; |
| 177 | } |
| 178 | |
| 179 | static void in_cache_put(in_cache_entry *entry) |
| 180 | { |
| 181 | if (atomic_dec_and_test(&entry->use)) { |
| 182 | memset(entry, 0, sizeof(in_cache_entry)); |
| 183 | kfree(entry); |
| 184 | } |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * This should be called with write lock on |
| 191 | */ |
| 192 | static void in_cache_remove_entry(in_cache_entry *entry, |
| 193 | struct mpoa_client *client) |
| 194 | { |
| 195 | struct atm_vcc *vcc; |
| 196 | struct k_message msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | vcc = entry->shortcut; |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 199 | dprintk("removing an ingress entry, ip = %pI4\n", |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 200 | &entry->ctrl_info.in_dst_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | if (entry->prev != NULL) |
| 203 | entry->prev->next = entry->next; |
| 204 | else |
| 205 | client->in_cache = entry->next; |
| 206 | if (entry->next != NULL) |
| 207 | entry->next->prev = entry->prev; |
| 208 | client->in_ops->put(entry); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 209 | if (client->in_cache == NULL && client->eg_cache == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | msg.type = STOP_KEEP_ALIVE_SM; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 211 | msg_to_mpoad(&msg, client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Check if the egress side still uses this VCC */ |
| 215 | if (vcc != NULL) { |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 216 | eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc, |
| 217 | client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (eg_entry != NULL) { |
| 219 | client->eg_ops->put(eg_entry); |
| 220 | return; |
| 221 | } |
| 222 | vcc_release_async(vcc, -EPIPE); |
| 223 | } |
| 224 | |
| 225 | return; |
| 226 | } |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* Call this every MPC-p2 seconds... Not exactly correct solution, |
| 229 | but an easy one... */ |
| 230 | static void clear_count_and_expired(struct mpoa_client *client) |
| 231 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | in_cache_entry *entry, *next_entry; |
| 233 | struct timeval now; |
| 234 | |
| 235 | do_gettimeofday(&now); |
| 236 | |
| 237 | write_lock_bh(&client->ingress_lock); |
| 238 | entry = client->in_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 239 | while (entry != NULL) { |
| 240 | entry->count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | next_entry = entry->next; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 242 | if ((now.tv_sec - entry->tv.tv_sec) |
| 243 | > entry->ctrl_info.holding_time) { |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 244 | dprintk("holding time expired, ip = %pI4\n", |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 245 | &entry->ctrl_info.in_dst_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | client->in_ops->remove_entry(entry, client); |
| 247 | } |
| 248 | entry = next_entry; |
| 249 | } |
| 250 | write_unlock_bh(&client->ingress_lock); |
| 251 | |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | /* Call this every MPC-p4 seconds. */ |
| 256 | static void check_resolving_entries(struct mpoa_client *client) |
| 257 | { |
| 258 | |
| 259 | struct atm_mpoa_qos *qos; |
| 260 | in_cache_entry *entry; |
| 261 | struct timeval now; |
| 262 | struct k_message msg; |
| 263 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 264 | do_gettimeofday(&now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | read_lock_bh(&client->ingress_lock); |
| 267 | entry = client->in_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 268 | while (entry != NULL) { |
| 269 | if (entry->entry_state == INGRESS_RESOLVING) { |
| 270 | if ((now.tv_sec - entry->hold_down.tv_sec) < |
| 271 | client->parameters.mpc_p6) { |
| 272 | entry = entry->next; /* Entry in hold down */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | continue; |
| 274 | } |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 275 | if ((now.tv_sec - entry->reply_wait.tv_sec) > |
| 276 | entry->retry_time) { |
| 277 | entry->retry_time = MPC_C1 * (entry->retry_time); |
| 278 | /* |
| 279 | * Retry time maximum exceeded, |
| 280 | * put entry in hold down. |
| 281 | */ |
| 282 | if (entry->retry_time > client->parameters.mpc_p5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | do_gettimeofday(&(entry->hold_down)); |
| 284 | entry->retry_time = client->parameters.mpc_p4; |
| 285 | entry = entry->next; |
| 286 | continue; |
| 287 | } |
| 288 | /* Ask daemon to send a resolution request. */ |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 289 | memset(&(entry->hold_down), 0, sizeof(struct timeval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | msg.type = SND_MPOA_RES_RTRY; |
| 291 | memcpy(msg.MPS_ctrl, client->mps_ctrl_addr, ATM_ESA_LEN); |
| 292 | msg.content.in_info = entry->ctrl_info; |
| 293 | qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 294 | if (qos != NULL) |
| 295 | msg.qos = qos->qos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | msg_to_mpoad(&msg, client); |
| 297 | do_gettimeofday(&(entry->reply_wait)); |
| 298 | } |
| 299 | } |
| 300 | entry = entry->next; |
| 301 | } |
| 302 | read_unlock_bh(&client->ingress_lock); |
| 303 | } |
| 304 | |
| 305 | /* Call this every MPC-p5 seconds. */ |
| 306 | static void refresh_entries(struct mpoa_client *client) |
| 307 | { |
| 308 | struct timeval now; |
| 309 | struct in_cache_entry *entry = client->in_cache; |
| 310 | |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 311 | ddprintk("refresh_entries\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | do_gettimeofday(&now); |
| 313 | |
| 314 | read_lock_bh(&client->ingress_lock); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 315 | while (entry != NULL) { |
| 316 | if (entry->entry_state == INGRESS_RESOLVED) { |
| 317 | if (!(entry->refresh_time)) |
| 318 | entry->refresh_time = (2 * (entry->ctrl_info.holding_time))/3; |
| 319 | if ((now.tv_sec - entry->reply_wait.tv_sec) > |
| 320 | entry->refresh_time) { |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 321 | dprintk("refreshing an entry.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | entry->entry_state = INGRESS_REFRESHING; |
| 323 | |
| 324 | } |
| 325 | } |
| 326 | entry = entry->next; |
| 327 | } |
| 328 | read_unlock_bh(&client->ingress_lock); |
| 329 | } |
| 330 | |
| 331 | static void in_destroy_cache(struct mpoa_client *mpc) |
| 332 | { |
| 333 | write_lock_irq(&mpc->ingress_lock); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 334 | while (mpc->in_cache != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | mpc->in_ops->remove_entry(mpc->in_cache, mpc); |
| 336 | write_unlock_irq(&mpc->ingress_lock); |
| 337 | |
| 338 | return; |
| 339 | } |
| 340 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 341 | static eg_cache_entry *eg_cache_get_by_cache_id(__be32 cache_id, |
| 342 | struct mpoa_client *mpc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | eg_cache_entry *entry; |
| 345 | |
| 346 | read_lock_irq(&mpc->egress_lock); |
| 347 | entry = mpc->eg_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 348 | while (entry != NULL) { |
| 349 | if (entry->ctrl_info.cache_id == cache_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | atomic_inc(&entry->use); |
| 351 | read_unlock_irq(&mpc->egress_lock); |
| 352 | return entry; |
| 353 | } |
| 354 | entry = entry->next; |
| 355 | } |
| 356 | read_unlock_irq(&mpc->egress_lock); |
| 357 | |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | /* This can be called from any context since it saves CPU flags */ |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame] | 362 | static eg_cache_entry *eg_cache_get_by_tag(__be32 tag, struct mpoa_client *mpc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | unsigned long flags; |
| 365 | eg_cache_entry *entry; |
| 366 | |
| 367 | read_lock_irqsave(&mpc->egress_lock, flags); |
| 368 | entry = mpc->eg_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 369 | while (entry != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (entry->ctrl_info.tag == tag) { |
| 371 | atomic_inc(&entry->use); |
| 372 | read_unlock_irqrestore(&mpc->egress_lock, flags); |
| 373 | return entry; |
| 374 | } |
| 375 | entry = entry->next; |
| 376 | } |
| 377 | read_unlock_irqrestore(&mpc->egress_lock, flags); |
| 378 | |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | /* This can be called from any context since it saves CPU flags */ |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 383 | static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc, |
| 384 | struct mpoa_client *mpc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
| 386 | unsigned long flags; |
| 387 | eg_cache_entry *entry; |
| 388 | |
| 389 | read_lock_irqsave(&mpc->egress_lock, flags); |
| 390 | entry = mpc->eg_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 391 | while (entry != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (entry->shortcut == vcc) { |
| 393 | atomic_inc(&entry->use); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 394 | read_unlock_irqrestore(&mpc->egress_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return entry; |
| 396 | } |
| 397 | entry = entry->next; |
| 398 | } |
| 399 | read_unlock_irqrestore(&mpc->egress_lock, flags); |
| 400 | |
| 401 | return NULL; |
| 402 | } |
| 403 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 404 | static eg_cache_entry *eg_cache_get_by_src_ip(__be32 ipaddr, |
| 405 | struct mpoa_client *mpc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | eg_cache_entry *entry; |
| 408 | |
| 409 | read_lock_irq(&mpc->egress_lock); |
| 410 | entry = mpc->eg_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 411 | while (entry != NULL) { |
| 412 | if (entry->latest_ip_addr == ipaddr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | atomic_inc(&entry->use); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 414 | read_unlock_irq(&mpc->egress_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return entry; |
| 416 | } |
| 417 | entry = entry->next; |
| 418 | } |
| 419 | read_unlock_irq(&mpc->egress_lock); |
| 420 | |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | static void eg_cache_put(eg_cache_entry *entry) |
| 425 | { |
| 426 | if (atomic_dec_and_test(&entry->use)) { |
| 427 | memset(entry, 0, sizeof(eg_cache_entry)); |
| 428 | kfree(entry); |
| 429 | } |
| 430 | |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * This should be called with write lock on |
| 436 | */ |
| 437 | static void eg_cache_remove_entry(eg_cache_entry *entry, |
| 438 | struct mpoa_client *client) |
| 439 | { |
| 440 | struct atm_vcc *vcc; |
| 441 | struct k_message msg; |
| 442 | |
| 443 | vcc = entry->shortcut; |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 444 | dprintk("removing an egress entry.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | if (entry->prev != NULL) |
| 446 | entry->prev->next = entry->next; |
| 447 | else |
| 448 | client->eg_cache = entry->next; |
| 449 | if (entry->next != NULL) |
| 450 | entry->next->prev = entry->prev; |
| 451 | client->eg_ops->put(entry); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 452 | if (client->in_cache == NULL && client->eg_cache == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | msg.type = STOP_KEEP_ALIVE_SM; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 454 | msg_to_mpoad(&msg, client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Check if the ingress side still uses this VCC */ |
| 458 | if (vcc != NULL) { |
| 459 | in_cache_entry *in_entry = client->in_ops->get_by_vcc(vcc, client); |
| 460 | if (in_entry != NULL) { |
| 461 | client->in_ops->put(in_entry); |
| 462 | return; |
| 463 | } |
| 464 | vcc_release_async(vcc, -EPIPE); |
| 465 | } |
| 466 | |
| 467 | return; |
| 468 | } |
| 469 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 470 | static eg_cache_entry *eg_cache_add_entry(struct k_message *msg, |
| 471 | struct mpoa_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Arnaldo Carvalho de Melo | 2afe37c | 2006-11-21 01:14:33 -0200 | [diff] [blame] | 473 | eg_cache_entry *entry = kzalloc(sizeof(eg_cache_entry), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | if (entry == NULL) { |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 476 | pr_info("out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | return NULL; |
| 478 | } |
| 479 | |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 480 | dprintk("adding an egress entry, ip = %pI4, this should be our IP\n", |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 481 | &msg->content.eg_info.eg_dst_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
| 483 | atomic_set(&entry->use, 1); |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 484 | dprintk("new_eg_cache_entry: about to lock\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | write_lock_irq(&client->egress_lock); |
| 486 | entry->next = client->eg_cache; |
| 487 | entry->prev = NULL; |
| 488 | if (client->eg_cache != NULL) |
| 489 | client->eg_cache->prev = entry; |
| 490 | client->eg_cache = entry; |
| 491 | |
| 492 | memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN); |
| 493 | entry->ctrl_info = msg->content.eg_info; |
| 494 | do_gettimeofday(&(entry->tv)); |
| 495 | entry->entry_state = EGRESS_RESOLVED; |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 496 | dprintk("new_eg_cache_entry cache_id %u\n", |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 497 | ntohl(entry->ctrl_info.cache_id)); |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 498 | dprintk("mps_ip = %pI4\n", &entry->ctrl_info.mps_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | atomic_inc(&entry->use); |
| 500 | |
| 501 | write_unlock_irq(&client->egress_lock); |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 502 | dprintk("new_eg_cache_entry: unlocked\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | return entry; |
| 505 | } |
| 506 | |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 507 | static void update_eg_cache_entry(eg_cache_entry *entry, uint16_t holding_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
| 509 | do_gettimeofday(&(entry->tv)); |
| 510 | entry->entry_state = EGRESS_RESOLVED; |
| 511 | entry->ctrl_info.holding_time = holding_time; |
| 512 | |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | static void clear_expired(struct mpoa_client *client) |
| 517 | { |
| 518 | eg_cache_entry *entry, *next_entry; |
| 519 | struct timeval now; |
| 520 | struct k_message msg; |
| 521 | |
| 522 | do_gettimeofday(&now); |
| 523 | |
| 524 | write_lock_irq(&client->egress_lock); |
| 525 | entry = client->eg_cache; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 526 | while (entry != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | next_entry = entry->next; |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 528 | if ((now.tv_sec - entry->tv.tv_sec) |
| 529 | > entry->ctrl_info.holding_time) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | msg.type = SND_EGRESS_PURGE; |
| 531 | msg.content.eg_info = entry->ctrl_info; |
Joe Perches | b50c2ea | 2010-01-26 11:40:20 +0000 | [diff] [blame] | 532 | dprintk("egress_cache: holding time expired, cache_id = %u.\n", |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 533 | ntohl(entry->ctrl_info.cache_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | msg_to_mpoad(&msg, client); |
| 535 | client->eg_ops->remove_entry(entry, client); |
| 536 | } |
| 537 | entry = next_entry; |
| 538 | } |
| 539 | write_unlock_irq(&client->egress_lock); |
| 540 | |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | static void eg_destroy_cache(struct mpoa_client *mpc) |
| 545 | { |
| 546 | write_lock_irq(&mpc->egress_lock); |
Joe Perches | bee67d3 | 2010-01-26 11:40:10 +0000 | [diff] [blame] | 547 | while (mpc->eg_cache != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | mpc->eg_ops->remove_entry(mpc->eg_cache, mpc); |
| 549 | write_unlock_irq(&mpc->egress_lock); |
| 550 | |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | static struct in_cache_ops ingress_ops = { |
| 556 | in_cache_add_entry, /* add_entry */ |
| 557 | in_cache_get, /* get */ |
| 558 | in_cache_get_with_mask, /* get_with_mask */ |
| 559 | in_cache_get_by_vcc, /* get_by_vcc */ |
| 560 | in_cache_put, /* put */ |
| 561 | in_cache_remove_entry, /* remove_entry */ |
| 562 | cache_hit, /* cache_hit */ |
| 563 | clear_count_and_expired, /* clear_count */ |
| 564 | check_resolving_entries, /* check_resolving */ |
| 565 | refresh_entries, /* refresh */ |
| 566 | in_destroy_cache /* destroy_cache */ |
| 567 | }; |
| 568 | |
| 569 | static struct eg_cache_ops egress_ops = { |
| 570 | eg_cache_add_entry, /* add_entry */ |
| 571 | eg_cache_get_by_cache_id, /* get_by_cache_id */ |
| 572 | eg_cache_get_by_tag, /* get_by_tag */ |
| 573 | eg_cache_get_by_vcc, /* get_by_vcc */ |
| 574 | eg_cache_get_by_src_ip, /* get_by_src_ip */ |
| 575 | eg_cache_put, /* put */ |
| 576 | eg_cache_remove_entry, /* remove_entry */ |
| 577 | update_eg_cache_entry, /* update */ |
| 578 | clear_expired, /* clear_expired */ |
| 579 | eg_destroy_cache /* destroy_cache */ |
| 580 | }; |
| 581 | |
| 582 | |
| 583 | void atm_mpoa_init_cache(struct mpoa_client *mpc) |
| 584 | { |
| 585 | mpc->in_ops = &ingress_ops; |
| 586 | mpc->eg_ops = &egress_ops; |
| 587 | |
| 588 | return; |
| 589 | } |