controlmsg timerfunc delay
[cor_2_6_31.git] / net / atm / mpoa_caches.c
blob4504a4b339bb8fb66e4c2e743815462157984c95
1 #include <linux/types.h>
2 #include <linux/atmmpc.h>
3 #include <linux/time.h>
5 #include "mpoa_caches.h"
6 #include "mpc.h"
8 /*
9 * mpoa_caches.c: Implementation of ingress and egress cache
10 * handling functions
13 #if 0
14 #define dprintk printk /* debug */
15 #else
16 #define dprintk(format,args...)
17 #endif
19 #if 0
20 #define ddprintk printk /* more debug */
21 #else
22 #define ddprintk(format,args...)
23 #endif
25 static in_cache_entry *in_cache_get(__be32 dst_ip,
26 struct mpoa_client *client)
28 in_cache_entry *entry;
30 read_lock_bh(&client->ingress_lock);
31 entry = client->in_cache;
32 while(entry != NULL){
33 if( entry->ctrl_info.in_dst_ip == dst_ip ){
34 atomic_inc(&entry->use);
35 read_unlock_bh(&client->ingress_lock);
36 return entry;
38 entry = entry->next;
40 read_unlock_bh(&client->ingress_lock);
42 return NULL;
45 static in_cache_entry *in_cache_get_with_mask(__be32 dst_ip,
46 struct mpoa_client *client,
47 __be32 mask)
49 in_cache_entry *entry;
51 read_lock_bh(&client->ingress_lock);
52 entry = client->in_cache;
53 while(entry != NULL){
54 if((entry->ctrl_info.in_dst_ip & mask) == (dst_ip & mask )){
55 atomic_inc(&entry->use);
56 read_unlock_bh(&client->ingress_lock);
57 return entry;
59 entry = entry->next;
61 read_unlock_bh(&client->ingress_lock);
63 return NULL;
67 static in_cache_entry *in_cache_get_by_vcc(struct atm_vcc *vcc,
68 struct mpoa_client *client )
70 in_cache_entry *entry;
72 read_lock_bh(&client->ingress_lock);
73 entry = client->in_cache;
74 while(entry != NULL){
75 if(entry->shortcut == vcc) {
76 atomic_inc(&entry->use);
77 read_unlock_bh(&client->ingress_lock);
78 return entry;
80 entry = entry->next;
82 read_unlock_bh(&client->ingress_lock);
84 return NULL;
87 static in_cache_entry *in_cache_add_entry(__be32 dst_ip,
88 struct mpoa_client *client)
90 in_cache_entry *entry = kzalloc(sizeof(in_cache_entry), GFP_KERNEL);
92 if (entry == NULL) {
93 printk("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n");
94 return NULL;
97 dprintk("mpoa: mpoa_caches.c: adding an ingress entry, ip = %pI4\n", &dst_ip);
99 atomic_set(&entry->use, 1);
100 dprintk("mpoa: mpoa_caches.c: new_in_cache_entry: about to lock\n");
101 write_lock_bh(&client->ingress_lock);
102 entry->next = client->in_cache;
103 entry->prev = NULL;
104 if (client->in_cache != NULL)
105 client->in_cache->prev = entry;
106 client->in_cache = entry;
108 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
109 entry->ctrl_info.in_dst_ip = dst_ip;
110 do_gettimeofday(&(entry->tv));
111 entry->retry_time = client->parameters.mpc_p4;
112 entry->count = 1;
113 entry->entry_state = INGRESS_INVALID;
114 entry->ctrl_info.holding_time = HOLDING_TIME_DEFAULT;
115 atomic_inc(&entry->use);
117 write_unlock_bh(&client->ingress_lock);
118 dprintk("mpoa: mpoa_caches.c: new_in_cache_entry: unlocked\n");
120 return entry;
123 static int cache_hit(in_cache_entry *entry, struct mpoa_client *mpc)
125 struct atm_mpoa_qos *qos;
126 struct k_message msg;
128 entry->count++;
129 if(entry->entry_state == INGRESS_RESOLVED && entry->shortcut != NULL)
130 return OPEN;
132 if(entry->entry_state == INGRESS_REFRESHING){
133 if(entry->count > mpc->parameters.mpc_p1){
134 msg.type = SND_MPOA_RES_RQST;
135 msg.content.in_info = entry->ctrl_info;
136 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
137 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
138 if (qos != NULL) msg.qos = qos->qos;
139 msg_to_mpoad(&msg, mpc);
140 do_gettimeofday(&(entry->reply_wait));
141 entry->entry_state = INGRESS_RESOLVING;
143 if(entry->shortcut != NULL)
144 return OPEN;
145 return CLOSED;
148 if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL)
149 return OPEN;
151 if( entry->count > mpc->parameters.mpc_p1 &&
152 entry->entry_state == INGRESS_INVALID){
153 dprintk("mpoa: (%s) mpoa_caches.c: threshold exceeded for ip %pI4, sending MPOA res req\n",
154 mpc->dev->name, &entry->ctrl_info.in_dst_ip);
155 entry->entry_state = INGRESS_RESOLVING;
156 msg.type = SND_MPOA_RES_RQST;
157 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN );
158 msg.content.in_info = entry->ctrl_info;
159 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
160 if (qos != NULL) msg.qos = qos->qos;
161 msg_to_mpoad( &msg, mpc);
162 do_gettimeofday(&(entry->reply_wait));
165 return CLOSED;
168 static void in_cache_put(in_cache_entry *entry)
170 if (atomic_dec_and_test(&entry->use)) {
171 memset(entry, 0, sizeof(in_cache_entry));
172 kfree(entry);
175 return;
179 * This should be called with write lock on
181 static void in_cache_remove_entry(in_cache_entry *entry,
182 struct mpoa_client *client)
184 struct atm_vcc *vcc;
185 struct k_message msg;
187 vcc = entry->shortcut;
188 dprintk("mpoa: mpoa_caches.c: removing an ingress entry, ip = %pI4\n",
189 &entry->ctrl_info.in_dst_ip);
191 if (entry->prev != NULL)
192 entry->prev->next = entry->next;
193 else
194 client->in_cache = entry->next;
195 if (entry->next != NULL)
196 entry->next->prev = entry->prev;
197 client->in_ops->put(entry);
198 if(client->in_cache == NULL && client->eg_cache == NULL){
199 msg.type = STOP_KEEP_ALIVE_SM;
200 msg_to_mpoad(&msg,client);
203 /* Check if the egress side still uses this VCC */
204 if (vcc != NULL) {
205 eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc, client);
206 if (eg_entry != NULL) {
207 client->eg_ops->put(eg_entry);
208 return;
210 vcc_release_async(vcc, -EPIPE);
213 return;
217 /* Call this every MPC-p2 seconds... Not exactly correct solution,
218 but an easy one... */
219 static void clear_count_and_expired(struct mpoa_client *client)
221 in_cache_entry *entry, *next_entry;
222 struct timeval now;
224 do_gettimeofday(&now);
226 write_lock_bh(&client->ingress_lock);
227 entry = client->in_cache;
228 while(entry != NULL){
229 entry->count=0;
230 next_entry = entry->next;
231 if((now.tv_sec - entry->tv.tv_sec)
232 > entry->ctrl_info.holding_time){
233 dprintk("mpoa: mpoa_caches.c: holding time expired, ip = %pI4\n",
234 &entry->ctrl_info.in_dst_ip);
235 client->in_ops->remove_entry(entry, client);
237 entry = next_entry;
239 write_unlock_bh(&client->ingress_lock);
241 return;
244 /* Call this every MPC-p4 seconds. */
245 static void check_resolving_entries(struct mpoa_client *client)
248 struct atm_mpoa_qos *qos;
249 in_cache_entry *entry;
250 struct timeval now;
251 struct k_message msg;
253 do_gettimeofday( &now );
255 read_lock_bh(&client->ingress_lock);
256 entry = client->in_cache;
257 while( entry != NULL ){
258 if(entry->entry_state == INGRESS_RESOLVING){
259 if(now.tv_sec - entry->hold_down.tv_sec < client->parameters.mpc_p6){
260 entry = entry->next; /* Entry in hold down */
261 continue;
263 if( (now.tv_sec - entry->reply_wait.tv_sec) >
264 entry->retry_time ){
265 entry->retry_time = MPC_C1*( entry->retry_time );
266 if(entry->retry_time > client->parameters.mpc_p5){
267 /* Retry time maximum exceeded, put entry in hold down. */
268 do_gettimeofday(&(entry->hold_down));
269 entry->retry_time = client->parameters.mpc_p4;
270 entry = entry->next;
271 continue;
273 /* Ask daemon to send a resolution request. */
274 memset(&(entry->hold_down),0,sizeof(struct timeval));
275 msg.type = SND_MPOA_RES_RTRY;
276 memcpy(msg.MPS_ctrl, client->mps_ctrl_addr, ATM_ESA_LEN);
277 msg.content.in_info = entry->ctrl_info;
278 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
279 if (qos != NULL) msg.qos = qos->qos;
280 msg_to_mpoad(&msg, client);
281 do_gettimeofday(&(entry->reply_wait));
284 entry = entry->next;
286 read_unlock_bh(&client->ingress_lock);
289 /* Call this every MPC-p5 seconds. */
290 static void refresh_entries(struct mpoa_client *client)
292 struct timeval now;
293 struct in_cache_entry *entry = client->in_cache;
295 ddprintk("mpoa: mpoa_caches.c: refresh_entries\n");
296 do_gettimeofday(&now);
298 read_lock_bh(&client->ingress_lock);
299 while( entry != NULL ){
300 if( entry->entry_state == INGRESS_RESOLVED ){
301 if(!(entry->refresh_time))
302 entry->refresh_time = (2*(entry->ctrl_info.holding_time))/3;
303 if( (now.tv_sec - entry->reply_wait.tv_sec) > entry->refresh_time ){
304 dprintk("mpoa: mpoa_caches.c: refreshing an entry.\n");
305 entry->entry_state = INGRESS_REFRESHING;
309 entry = entry->next;
311 read_unlock_bh(&client->ingress_lock);
314 static void in_destroy_cache(struct mpoa_client *mpc)
316 write_lock_irq(&mpc->ingress_lock);
317 while(mpc->in_cache != NULL)
318 mpc->in_ops->remove_entry(mpc->in_cache, mpc);
319 write_unlock_irq(&mpc->ingress_lock);
321 return;
324 static eg_cache_entry *eg_cache_get_by_cache_id(__be32 cache_id, struct mpoa_client *mpc)
326 eg_cache_entry *entry;
328 read_lock_irq(&mpc->egress_lock);
329 entry = mpc->eg_cache;
330 while(entry != NULL){
331 if(entry->ctrl_info.cache_id == cache_id){
332 atomic_inc(&entry->use);
333 read_unlock_irq(&mpc->egress_lock);
334 return entry;
336 entry = entry->next;
338 read_unlock_irq(&mpc->egress_lock);
340 return NULL;
343 /* This can be called from any context since it saves CPU flags */
344 static eg_cache_entry *eg_cache_get_by_tag(__be32 tag, struct mpoa_client *mpc)
346 unsigned long flags;
347 eg_cache_entry *entry;
349 read_lock_irqsave(&mpc->egress_lock, flags);
350 entry = mpc->eg_cache;
351 while (entry != NULL){
352 if (entry->ctrl_info.tag == tag) {
353 atomic_inc(&entry->use);
354 read_unlock_irqrestore(&mpc->egress_lock, flags);
355 return entry;
357 entry = entry->next;
359 read_unlock_irqrestore(&mpc->egress_lock, flags);
361 return NULL;
364 /* This can be called from any context since it saves CPU flags */
365 static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc, struct mpoa_client *mpc)
367 unsigned long flags;
368 eg_cache_entry *entry;
370 read_lock_irqsave(&mpc->egress_lock, flags);
371 entry = mpc->eg_cache;
372 while (entry != NULL){
373 if (entry->shortcut == vcc) {
374 atomic_inc(&entry->use);
375 read_unlock_irqrestore(&mpc->egress_lock, flags);
376 return entry;
378 entry = entry->next;
380 read_unlock_irqrestore(&mpc->egress_lock, flags);
382 return NULL;
385 static eg_cache_entry *eg_cache_get_by_src_ip(__be32 ipaddr, struct mpoa_client *mpc)
387 eg_cache_entry *entry;
389 read_lock_irq(&mpc->egress_lock);
390 entry = mpc->eg_cache;
391 while(entry != NULL){
392 if(entry->latest_ip_addr == ipaddr) {
393 atomic_inc(&entry->use);
394 read_unlock_irq(&mpc->egress_lock);
395 return entry;
397 entry = entry->next;
399 read_unlock_irq(&mpc->egress_lock);
401 return NULL;
404 static void eg_cache_put(eg_cache_entry *entry)
406 if (atomic_dec_and_test(&entry->use)) {
407 memset(entry, 0, sizeof(eg_cache_entry));
408 kfree(entry);
411 return;
415 * This should be called with write lock on
417 static void eg_cache_remove_entry(eg_cache_entry *entry,
418 struct mpoa_client *client)
420 struct atm_vcc *vcc;
421 struct k_message msg;
423 vcc = entry->shortcut;
424 dprintk("mpoa: mpoa_caches.c: removing an egress entry.\n");
425 if (entry->prev != NULL)
426 entry->prev->next = entry->next;
427 else
428 client->eg_cache = entry->next;
429 if (entry->next != NULL)
430 entry->next->prev = entry->prev;
431 client->eg_ops->put(entry);
432 if(client->in_cache == NULL && client->eg_cache == NULL){
433 msg.type = STOP_KEEP_ALIVE_SM;
434 msg_to_mpoad(&msg,client);
437 /* Check if the ingress side still uses this VCC */
438 if (vcc != NULL) {
439 in_cache_entry *in_entry = client->in_ops->get_by_vcc(vcc, client);
440 if (in_entry != NULL) {
441 client->in_ops->put(in_entry);
442 return;
444 vcc_release_async(vcc, -EPIPE);
447 return;
450 static eg_cache_entry *eg_cache_add_entry(struct k_message *msg, struct mpoa_client *client)
452 eg_cache_entry *entry = kzalloc(sizeof(eg_cache_entry), GFP_KERNEL);
454 if (entry == NULL) {
455 printk("mpoa: mpoa_caches.c: new_eg_cache_entry: out of memory\n");
456 return NULL;
459 dprintk("mpoa: mpoa_caches.c: adding an egress entry, ip = %pI4, this should be our IP\n",
460 &msg->content.eg_info.eg_dst_ip);
462 atomic_set(&entry->use, 1);
463 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry: about to lock\n");
464 write_lock_irq(&client->egress_lock);
465 entry->next = client->eg_cache;
466 entry->prev = NULL;
467 if (client->eg_cache != NULL)
468 client->eg_cache->prev = entry;
469 client->eg_cache = entry;
471 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
472 entry->ctrl_info = msg->content.eg_info;
473 do_gettimeofday(&(entry->tv));
474 entry->entry_state = EGRESS_RESOLVED;
475 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry cache_id %lu\n", ntohl(entry->ctrl_info.cache_id));
476 dprintk("mpoa: mpoa_caches.c: mps_ip = %pI4\n",
477 &entry->ctrl_info.mps_ip);
478 atomic_inc(&entry->use);
480 write_unlock_irq(&client->egress_lock);
481 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry: unlocked\n");
483 return entry;
486 static void update_eg_cache_entry(eg_cache_entry * entry, uint16_t holding_time)
488 do_gettimeofday(&(entry->tv));
489 entry->entry_state = EGRESS_RESOLVED;
490 entry->ctrl_info.holding_time = holding_time;
492 return;
495 static void clear_expired(struct mpoa_client *client)
497 eg_cache_entry *entry, *next_entry;
498 struct timeval now;
499 struct k_message msg;
501 do_gettimeofday(&now);
503 write_lock_irq(&client->egress_lock);
504 entry = client->eg_cache;
505 while(entry != NULL){
506 next_entry = entry->next;
507 if((now.tv_sec - entry->tv.tv_sec)
508 > entry->ctrl_info.holding_time){
509 msg.type = SND_EGRESS_PURGE;
510 msg.content.eg_info = entry->ctrl_info;
511 dprintk("mpoa: mpoa_caches.c: egress_cache: holding time expired, cache_id = %lu.\n",ntohl(entry->ctrl_info.cache_id));
512 msg_to_mpoad(&msg, client);
513 client->eg_ops->remove_entry(entry, client);
515 entry = next_entry;
517 write_unlock_irq(&client->egress_lock);
519 return;
522 static void eg_destroy_cache(struct mpoa_client *mpc)
524 write_lock_irq(&mpc->egress_lock);
525 while(mpc->eg_cache != NULL)
526 mpc->eg_ops->remove_entry(mpc->eg_cache, mpc);
527 write_unlock_irq(&mpc->egress_lock);
529 return;
534 static struct in_cache_ops ingress_ops = {
535 in_cache_add_entry, /* add_entry */
536 in_cache_get, /* get */
537 in_cache_get_with_mask, /* get_with_mask */
538 in_cache_get_by_vcc, /* get_by_vcc */
539 in_cache_put, /* put */
540 in_cache_remove_entry, /* remove_entry */
541 cache_hit, /* cache_hit */
542 clear_count_and_expired, /* clear_count */
543 check_resolving_entries, /* check_resolving */
544 refresh_entries, /* refresh */
545 in_destroy_cache /* destroy_cache */
548 static struct eg_cache_ops egress_ops = {
549 eg_cache_add_entry, /* add_entry */
550 eg_cache_get_by_cache_id, /* get_by_cache_id */
551 eg_cache_get_by_tag, /* get_by_tag */
552 eg_cache_get_by_vcc, /* get_by_vcc */
553 eg_cache_get_by_src_ip, /* get_by_src_ip */
554 eg_cache_put, /* put */
555 eg_cache_remove_entry, /* remove_entry */
556 update_eg_cache_entry, /* update */
557 clear_expired, /* clear_expired */
558 eg_destroy_cache /* destroy_cache */
562 void atm_mpoa_init_cache(struct mpoa_client *mpc)
564 mpc->in_ops = &ingress_ops;
565 mpc->eg_ops = &egress_ops;
567 return;