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