Linux 2.6.19-rc6
[cris-mirror.git] / net / atm / mpoa_caches.c
blobfbf13cdcf46ed2af4edfa2f1bf085af201e0ed6f
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(uint32_t 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(uint32_t dst_ip,
46 struct mpoa_client *client,
47 uint32_t 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(uint32_t dst_ip,
88 struct mpoa_client *client)
90 in_cache_entry* entry = kmalloc(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 = %u.%u.%u.%u\n", NIPQUAD(dst_ip));
98 memset(entry,0,sizeof(in_cache_entry));
100 atomic_set(&entry->use, 1);
101 dprintk("mpoa: mpoa_caches.c: new_in_cache_entry: about to lock\n");
102 write_lock_bh(&client->ingress_lock);
103 entry->next = client->in_cache;
104 entry->prev = NULL;
105 if (client->in_cache != NULL)
106 client->in_cache->prev = entry;
107 client->in_cache = entry;
109 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
110 entry->ctrl_info.in_dst_ip = dst_ip;
111 do_gettimeofday(&(entry->tv));
112 entry->retry_time = client->parameters.mpc_p4;
113 entry->count = 1;
114 entry->entry_state = INGRESS_INVALID;
115 entry->ctrl_info.holding_time = HOLDING_TIME_DEFAULT;
116 atomic_inc(&entry->use);
118 write_unlock_bh(&client->ingress_lock);
119 dprintk("mpoa: mpoa_caches.c: new_in_cache_entry: unlocked\n");
121 return entry;
124 static int cache_hit(in_cache_entry *entry, struct mpoa_client *mpc)
126 struct atm_mpoa_qos *qos;
127 struct k_message msg;
129 entry->count++;
130 if(entry->entry_state == INGRESS_RESOLVED && entry->shortcut != NULL)
131 return OPEN;
133 if(entry->entry_state == INGRESS_REFRESHING){
134 if(entry->count > mpc->parameters.mpc_p1){
135 msg.type = SND_MPOA_RES_RQST;
136 msg.content.in_info = entry->ctrl_info;
137 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
138 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
139 if (qos != NULL) msg.qos = qos->qos;
140 msg_to_mpoad(&msg, mpc);
141 do_gettimeofday(&(entry->reply_wait));
142 entry->entry_state = INGRESS_RESOLVING;
144 if(entry->shortcut != NULL)
145 return OPEN;
146 return CLOSED;
149 if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL)
150 return OPEN;
152 if( entry->count > mpc->parameters.mpc_p1 &&
153 entry->entry_state == INGRESS_INVALID){
154 dprintk("mpoa: (%s) mpoa_caches.c: threshold exceeded for ip %u.%u.%u.%u, sending MPOA res req\n", mpc->dev->name, NIPQUAD(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 = %u.%u.%u.%u\n",NIPQUAD(entry->ctrl_info.in_dst_ip));
190 if (entry->prev != NULL)
191 entry->prev->next = entry->next;
192 else
193 client->in_cache = entry->next;
194 if (entry->next != NULL)
195 entry->next->prev = entry->prev;
196 client->in_ops->put(entry);
197 if(client->in_cache == NULL && client->eg_cache == NULL){
198 msg.type = STOP_KEEP_ALIVE_SM;
199 msg_to_mpoad(&msg,client);
202 /* Check if the egress side still uses this VCC */
203 if (vcc != NULL) {
204 eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc, client);
205 if (eg_entry != NULL) {
206 client->eg_ops->put(eg_entry);
207 return;
209 vcc_release_async(vcc, -EPIPE);
212 return;
216 /* Call this every MPC-p2 seconds... Not exactly correct solution,
217 but an easy one... */
218 static void clear_count_and_expired(struct mpoa_client *client)
220 in_cache_entry *entry, *next_entry;
221 struct timeval now;
223 do_gettimeofday(&now);
225 write_lock_bh(&client->ingress_lock);
226 entry = client->in_cache;
227 while(entry != NULL){
228 entry->count=0;
229 next_entry = entry->next;
230 if((now.tv_sec - entry->tv.tv_sec)
231 > entry->ctrl_info.holding_time){
232 dprintk("mpoa: mpoa_caches.c: holding time expired, ip = %u.%u.%u.%u\n", NIPQUAD(entry->ctrl_info.in_dst_ip));
233 client->in_ops->remove_entry(entry, client);
235 entry = next_entry;
237 write_unlock_bh(&client->ingress_lock);
239 return;
242 /* Call this every MPC-p4 seconds. */
243 static void check_resolving_entries(struct mpoa_client *client)
246 struct atm_mpoa_qos *qos;
247 in_cache_entry *entry;
248 struct timeval now;
249 struct k_message msg;
251 do_gettimeofday( &now );
253 read_lock_bh(&client->ingress_lock);
254 entry = client->in_cache;
255 while( entry != NULL ){
256 if(entry->entry_state == INGRESS_RESOLVING){
257 if(now.tv_sec - entry->hold_down.tv_sec < client->parameters.mpc_p6){
258 entry = entry->next; /* Entry in hold down */
259 continue;
261 if( (now.tv_sec - entry->reply_wait.tv_sec) >
262 entry->retry_time ){
263 entry->retry_time = MPC_C1*( entry->retry_time );
264 if(entry->retry_time > client->parameters.mpc_p5){
265 /* Retry time maximum exceeded, put entry in hold down. */
266 do_gettimeofday(&(entry->hold_down));
267 entry->retry_time = client->parameters.mpc_p4;
268 entry = entry->next;
269 continue;
271 /* Ask daemon to send a resolution request. */
272 memset(&(entry->hold_down),0,sizeof(struct timeval));
273 msg.type = SND_MPOA_RES_RTRY;
274 memcpy(msg.MPS_ctrl, client->mps_ctrl_addr, ATM_ESA_LEN);
275 msg.content.in_info = entry->ctrl_info;
276 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
277 if (qos != NULL) msg.qos = qos->qos;
278 msg_to_mpoad(&msg, client);
279 do_gettimeofday(&(entry->reply_wait));
282 entry = entry->next;
284 read_unlock_bh(&client->ingress_lock);
287 /* Call this every MPC-p5 seconds. */
288 static void refresh_entries(struct mpoa_client *client)
290 struct timeval now;
291 struct in_cache_entry *entry = client->in_cache;
293 ddprintk("mpoa: mpoa_caches.c: refresh_entries\n");
294 do_gettimeofday(&now);
296 read_lock_bh(&client->ingress_lock);
297 while( entry != NULL ){
298 if( entry->entry_state == INGRESS_RESOLVED ){
299 if(!(entry->refresh_time))
300 entry->refresh_time = (2*(entry->ctrl_info.holding_time))/3;
301 if( (now.tv_sec - entry->reply_wait.tv_sec) > entry->refresh_time ){
302 dprintk("mpoa: mpoa_caches.c: refreshing an entry.\n");
303 entry->entry_state = INGRESS_REFRESHING;
307 entry = entry->next;
309 read_unlock_bh(&client->ingress_lock);
312 static void in_destroy_cache(struct mpoa_client *mpc)
314 write_lock_irq(&mpc->ingress_lock);
315 while(mpc->in_cache != NULL)
316 mpc->in_ops->remove_entry(mpc->in_cache, mpc);
317 write_unlock_irq(&mpc->ingress_lock);
319 return;
322 static eg_cache_entry *eg_cache_get_by_cache_id(uint32_t cache_id, struct mpoa_client *mpc)
324 eg_cache_entry *entry;
326 read_lock_irq(&mpc->egress_lock);
327 entry = mpc->eg_cache;
328 while(entry != NULL){
329 if(entry->ctrl_info.cache_id == cache_id){
330 atomic_inc(&entry->use);
331 read_unlock_irq(&mpc->egress_lock);
332 return entry;
334 entry = entry->next;
336 read_unlock_irq(&mpc->egress_lock);
338 return NULL;
341 /* This can be called from any context since it saves CPU flags */
342 static eg_cache_entry *eg_cache_get_by_tag(uint32_t tag, struct mpoa_client *mpc)
344 unsigned long flags;
345 eg_cache_entry *entry;
347 read_lock_irqsave(&mpc->egress_lock, flags);
348 entry = mpc->eg_cache;
349 while (entry != NULL){
350 if (entry->ctrl_info.tag == tag) {
351 atomic_inc(&entry->use);
352 read_unlock_irqrestore(&mpc->egress_lock, flags);
353 return entry;
355 entry = entry->next;
357 read_unlock_irqrestore(&mpc->egress_lock, flags);
359 return NULL;
362 /* This can be called from any context since it saves CPU flags */
363 static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc, struct mpoa_client *mpc)
365 unsigned long flags;
366 eg_cache_entry *entry;
368 read_lock_irqsave(&mpc->egress_lock, flags);
369 entry = mpc->eg_cache;
370 while (entry != NULL){
371 if (entry->shortcut == vcc) {
372 atomic_inc(&entry->use);
373 read_unlock_irqrestore(&mpc->egress_lock, flags);
374 return entry;
376 entry = entry->next;
378 read_unlock_irqrestore(&mpc->egress_lock, flags);
380 return NULL;
383 static eg_cache_entry *eg_cache_get_by_src_ip(uint32_t ipaddr, struct mpoa_client *mpc)
385 eg_cache_entry *entry;
387 read_lock_irq(&mpc->egress_lock);
388 entry = mpc->eg_cache;
389 while(entry != NULL){
390 if(entry->latest_ip_addr == ipaddr) {
391 atomic_inc(&entry->use);
392 read_unlock_irq(&mpc->egress_lock);
393 return entry;
395 entry = entry->next;
397 read_unlock_irq(&mpc->egress_lock);
399 return NULL;
402 static void eg_cache_put(eg_cache_entry *entry)
404 if (atomic_dec_and_test(&entry->use)) {
405 memset(entry, 0, sizeof(eg_cache_entry));
406 kfree(entry);
409 return;
413 * This should be called with write lock on
415 static void eg_cache_remove_entry(eg_cache_entry *entry,
416 struct mpoa_client *client)
418 struct atm_vcc *vcc;
419 struct k_message msg;
421 vcc = entry->shortcut;
422 dprintk("mpoa: mpoa_caches.c: removing an egress entry.\n");
423 if (entry->prev != NULL)
424 entry->prev->next = entry->next;
425 else
426 client->eg_cache = entry->next;
427 if (entry->next != NULL)
428 entry->next->prev = entry->prev;
429 client->eg_ops->put(entry);
430 if(client->in_cache == NULL && client->eg_cache == NULL){
431 msg.type = STOP_KEEP_ALIVE_SM;
432 msg_to_mpoad(&msg,client);
435 /* Check if the ingress side still uses this VCC */
436 if (vcc != NULL) {
437 in_cache_entry *in_entry = client->in_ops->get_by_vcc(vcc, client);
438 if (in_entry != NULL) {
439 client->in_ops->put(in_entry);
440 return;
442 vcc_release_async(vcc, -EPIPE);
445 return;
448 static eg_cache_entry *eg_cache_add_entry(struct k_message *msg, struct mpoa_client *client)
450 eg_cache_entry *entry = kmalloc(sizeof(eg_cache_entry), GFP_KERNEL);
452 if (entry == NULL) {
453 printk("mpoa: mpoa_caches.c: new_eg_cache_entry: out of memory\n");
454 return NULL;
457 dprintk("mpoa: mpoa_caches.c: adding an egress entry, ip = %u.%u.%u.%u, this should be our IP\n", NIPQUAD(msg->content.eg_info.eg_dst_ip));
458 memset(entry, 0, sizeof(eg_cache_entry));
460 atomic_set(&entry->use, 1);
461 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry: about to lock\n");
462 write_lock_irq(&client->egress_lock);
463 entry->next = client->eg_cache;
464 entry->prev = NULL;
465 if (client->eg_cache != NULL)
466 client->eg_cache->prev = entry;
467 client->eg_cache = entry;
469 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
470 entry->ctrl_info = msg->content.eg_info;
471 do_gettimeofday(&(entry->tv));
472 entry->entry_state = EGRESS_RESOLVED;
473 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry cache_id %lu\n", ntohl(entry->ctrl_info.cache_id));
474 dprintk("mpoa: mpoa_caches.c: mps_ip = %u.%u.%u.%u\n",
475 NIPQUAD(entry->ctrl_info.mps_ip));
476 atomic_inc(&entry->use);
478 write_unlock_irq(&client->egress_lock);
479 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry: unlocked\n");
481 return entry;
484 static void update_eg_cache_entry(eg_cache_entry * entry, uint16_t holding_time)
486 do_gettimeofday(&(entry->tv));
487 entry->entry_state = EGRESS_RESOLVED;
488 entry->ctrl_info.holding_time = holding_time;
490 return;
493 static void clear_expired(struct mpoa_client *client)
495 eg_cache_entry *entry, *next_entry;
496 struct timeval now;
497 struct k_message msg;
499 do_gettimeofday(&now);
501 write_lock_irq(&client->egress_lock);
502 entry = client->eg_cache;
503 while(entry != NULL){
504 next_entry = entry->next;
505 if((now.tv_sec - entry->tv.tv_sec)
506 > entry->ctrl_info.holding_time){
507 msg.type = SND_EGRESS_PURGE;
508 msg.content.eg_info = entry->ctrl_info;
509 dprintk("mpoa: mpoa_caches.c: egress_cache: holding time expired, cache_id = %lu.\n",ntohl(entry->ctrl_info.cache_id));
510 msg_to_mpoad(&msg, client);
511 client->eg_ops->remove_entry(entry, client);
513 entry = next_entry;
515 write_unlock_irq(&client->egress_lock);
517 return;
520 static void eg_destroy_cache(struct mpoa_client *mpc)
522 write_lock_irq(&mpc->egress_lock);
523 while(mpc->eg_cache != NULL)
524 mpc->eg_ops->remove_entry(mpc->eg_cache, mpc);
525 write_unlock_irq(&mpc->egress_lock);
527 return;
532 static struct in_cache_ops ingress_ops = {
533 in_cache_add_entry, /* add_entry */
534 in_cache_get, /* get */
535 in_cache_get_with_mask, /* get_with_mask */
536 in_cache_get_by_vcc, /* get_by_vcc */
537 in_cache_put, /* put */
538 in_cache_remove_entry, /* remove_entry */
539 cache_hit, /* cache_hit */
540 clear_count_and_expired, /* clear_count */
541 check_resolving_entries, /* check_resolving */
542 refresh_entries, /* refresh */
543 in_destroy_cache /* destroy_cache */
546 static struct eg_cache_ops egress_ops = {
547 eg_cache_add_entry, /* add_entry */
548 eg_cache_get_by_cache_id, /* get_by_cache_id */
549 eg_cache_get_by_tag, /* get_by_tag */
550 eg_cache_get_by_vcc, /* get_by_vcc */
551 eg_cache_get_by_src_ip, /* get_by_src_ip */
552 eg_cache_put, /* put */
553 eg_cache_remove_entry, /* remove_entry */
554 update_eg_cache_entry, /* update */
555 clear_expired, /* clear_expired */
556 eg_destroy_cache /* destroy_cache */
560 void atm_mpoa_init_cache(struct mpoa_client *mpc)
562 mpc->in_ops = &ingress_ops;
563 mpc->eg_ops = &egress_ops;
565 return;