1 #include <linux/types.h>
2 #include <linux/atmmpc.h>
3 #include <linux/time.h>
5 #include "mpoa_caches.h"
9 * mpoa_caches.c: Implementation of ingress and egress cache
14 #define dprintk printk /* debug */
16 #define dprintk(format,args...)
20 #define ddprintk printk /* more debug */
22 #define ddprintk(format,args...)
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
;
33 if( entry
->ctrl_info
.in_dst_ip
== dst_ip
){
34 atomic_inc(&entry
->use
);
35 read_unlock_bh(&client
->ingress_lock
);
40 read_unlock_bh(&client
->ingress_lock
);
45 static in_cache_entry
*in_cache_get_with_mask(uint32_t dst_ip
,
46 struct mpoa_client
*client
,
49 in_cache_entry
*entry
;
51 read_lock_bh(&client
->ingress_lock
);
52 entry
= client
->in_cache
;
54 if((entry
->ctrl_info
.in_dst_ip
& mask
) == (dst_ip
& mask
)){
55 atomic_inc(&entry
->use
);
56 read_unlock_bh(&client
->ingress_lock
);
61 read_unlock_bh(&client
->ingress_lock
);
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
;
75 if(entry
->shortcut
== vcc
) {
76 atomic_inc(&entry
->use
);
77 read_unlock_bh(&client
->ingress_lock
);
82 read_unlock_bh(&client
->ingress_lock
);
87 static in_cache_entry
*in_cache_add_entry(uint32_t dst_ip
,
88 struct mpoa_client
*client
)
90 unsigned char *ip
__attribute__ ((unused
)) = (unsigned char *)&dst_ip
;
91 in_cache_entry
* entry
= kmalloc(sizeof(in_cache_entry
), GFP_KERNEL
);
94 printk("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n");
98 dprintk("mpoa: mpoa_caches.c: adding an ingress entry, ip = %u.%u.%u.%u\n", ip
[0], ip
[1], ip
[2], ip
[3]);
99 memset(entry
,0,sizeof(in_cache_entry
));
101 atomic_set(&entry
->use
, 1);
102 dprintk("mpoa: mpoa_caches.c: new_in_cache_entry: about to lock\n");
103 write_lock_bh(&client
->ingress_lock
);
104 entry
->next
= client
->in_cache
;
106 if (client
->in_cache
!= NULL
)
107 client
->in_cache
->prev
= entry
;
108 client
->in_cache
= entry
;
110 memcpy(entry
->MPS_ctrl_ATM_addr
, client
->mps_ctrl_addr
, ATM_ESA_LEN
);
111 entry
->ctrl_info
.in_dst_ip
= dst_ip
;
112 do_gettimeofday(&(entry
->tv
));
113 entry
->retry_time
= client
->parameters
.mpc_p4
;
115 entry
->entry_state
= INGRESS_INVALID
;
116 entry
->ctrl_info
.holding_time
= HOLDING_TIME_DEFAULT
;
117 atomic_inc(&entry
->use
);
119 write_unlock_bh(&client
->ingress_lock
);
120 dprintk("mpoa: mpoa_caches.c: new_in_cache_entry: unlocked\n");
125 static int cache_hit(in_cache_entry
*entry
, struct mpoa_client
*mpc
)
127 struct atm_mpoa_qos
*qos
;
128 struct k_message msg
;
131 if(entry
->entry_state
== INGRESS_RESOLVED
&& entry
->shortcut
!= NULL
)
134 if(entry
->entry_state
== INGRESS_REFRESHING
){
135 if(entry
->count
> mpc
->parameters
.mpc_p1
){
136 msg
.type
= SND_MPOA_RES_RQST
;
137 msg
.content
.in_info
= entry
->ctrl_info
;
138 memcpy(msg
.MPS_ctrl
, mpc
->mps_ctrl_addr
, ATM_ESA_LEN
);
139 qos
= atm_mpoa_search_qos(entry
->ctrl_info
.in_dst_ip
);
140 if (qos
!= NULL
) msg
.qos
= qos
->qos
;
141 msg_to_mpoad(&msg
, mpc
);
142 do_gettimeofday(&(entry
->reply_wait
));
143 entry
->entry_state
= INGRESS_RESOLVING
;
145 if(entry
->shortcut
!= NULL
)
150 if(entry
->entry_state
== INGRESS_RESOLVING
&& entry
->shortcut
!= NULL
)
153 if( entry
->count
> mpc
->parameters
.mpc_p1
&&
154 entry
->entry_state
== INGRESS_INVALID
){
155 unsigned char *ip
__attribute__ ((unused
)) =
156 (unsigned char *)&entry
->ctrl_info
.in_dst_ip
;
158 dprintk("mpoa: (%s) mpoa_caches.c: threshold exceeded for ip %u.%u.%u.%u, sending MPOA res req\n", mpc
->dev
->name
, ip
[0], ip
[1], ip
[2], ip
[3]);
159 entry
->entry_state
= INGRESS_RESOLVING
;
160 msg
.type
= SND_MPOA_RES_RQST
;
161 memcpy(msg
.MPS_ctrl
, mpc
->mps_ctrl_addr
, ATM_ESA_LEN
);
162 msg
.content
.in_info
= entry
->ctrl_info
;
163 qos
= atm_mpoa_search_qos(entry
->ctrl_info
.in_dst_ip
);
164 if (qos
!= NULL
) msg
.qos
= qos
->qos
;
165 msg_to_mpoad( &msg
, mpc
);
166 do_gettimeofday(&(entry
->reply_wait
));
172 static void in_cache_put(in_cache_entry
*entry
)
174 if (atomic_dec_and_test(&entry
->use
)) {
175 memset(entry
, 0, sizeof(in_cache_entry
));
183 * This should be called with write lock on
185 static void in_cache_remove_entry(in_cache_entry
*entry
,
186 struct mpoa_client
*client
)
189 struct k_message msg
;
192 vcc
= entry
->shortcut
;
193 ip
= (unsigned char *)&entry
->ctrl_info
.in_dst_ip
;
194 dprintk("mpoa: mpoa_caches.c: removing an ingress entry, ip = %u.%u.%u.%u\n",ip
[0], ip
[1], ip
[2], ip
[3]);
196 if (entry
->prev
!= NULL
)
197 entry
->prev
->next
= entry
->next
;
199 client
->in_cache
= entry
->next
;
200 if (entry
->next
!= NULL
)
201 entry
->next
->prev
= entry
->prev
;
202 client
->in_ops
->put(entry
);
203 if(client
->in_cache
== NULL
&& client
->eg_cache
== NULL
){
204 msg
.type
= STOP_KEEP_ALIVE_SM
;
205 msg_to_mpoad(&msg
,client
);
208 /* Check if the egress side still uses this VCC */
210 eg_cache_entry
*eg_entry
= client
->eg_ops
->get_by_vcc(vcc
, client
);
211 if (eg_entry
!= NULL
) {
212 client
->eg_ops
->put(eg_entry
);
215 vcc_release_async(vcc
, -EPIPE
);
222 /* Call this every MPC-p2 seconds... Not exactly correct solution,
223 but an easy one... */
224 static void clear_count_and_expired(struct mpoa_client
*client
)
226 in_cache_entry
*entry
, *next_entry
;
229 do_gettimeofday(&now
);
231 write_lock_bh(&client
->ingress_lock
);
232 entry
= client
->in_cache
;
233 while(entry
!= NULL
){
235 next_entry
= entry
->next
;
236 if((now
.tv_sec
- entry
->tv
.tv_sec
)
237 > entry
->ctrl_info
.holding_time
){
238 dprintk("mpoa: mpoa_caches.c: holding time expired, ip = %u.%u.%u.%u\n", NIPQUAD(entry
->ctrl_info
.in_dst_ip
));
239 client
->in_ops
->remove_entry(entry
, client
);
243 write_unlock_bh(&client
->ingress_lock
);
248 /* Call this every MPC-p4 seconds. */
249 static void check_resolving_entries(struct mpoa_client
*client
)
252 struct atm_mpoa_qos
*qos
;
253 in_cache_entry
*entry
;
255 struct k_message msg
;
257 do_gettimeofday( &now
);
259 read_lock_bh(&client
->ingress_lock
);
260 entry
= client
->in_cache
;
261 while( entry
!= NULL
){
262 if(entry
->entry_state
== INGRESS_RESOLVING
){
263 if(now
.tv_sec
- entry
->hold_down
.tv_sec
< client
->parameters
.mpc_p6
){
264 entry
= entry
->next
; /* Entry in hold down */
267 if( (now
.tv_sec
- entry
->reply_wait
.tv_sec
) >
269 entry
->retry_time
= MPC_C1
*( entry
->retry_time
);
270 if(entry
->retry_time
> client
->parameters
.mpc_p5
){
271 /* Retry time maximum exceeded, put entry in hold down. */
272 do_gettimeofday(&(entry
->hold_down
));
273 entry
->retry_time
= client
->parameters
.mpc_p4
;
277 /* Ask daemon to send a resolution request. */
278 memset(&(entry
->hold_down
),0,sizeof(struct timeval
));
279 msg
.type
= SND_MPOA_RES_RTRY
;
280 memcpy(msg
.MPS_ctrl
, client
->mps_ctrl_addr
, ATM_ESA_LEN
);
281 msg
.content
.in_info
= entry
->ctrl_info
;
282 qos
= atm_mpoa_search_qos(entry
->ctrl_info
.in_dst_ip
);
283 if (qos
!= NULL
) msg
.qos
= qos
->qos
;
284 msg_to_mpoad(&msg
, client
);
285 do_gettimeofday(&(entry
->reply_wait
));
290 read_unlock_bh(&client
->ingress_lock
);
293 /* Call this every MPC-p5 seconds. */
294 static void refresh_entries(struct mpoa_client
*client
)
297 struct in_cache_entry
*entry
= client
->in_cache
;
299 ddprintk("mpoa: mpoa_caches.c: refresh_entries\n");
300 do_gettimeofday(&now
);
302 read_lock_bh(&client
->ingress_lock
);
303 while( entry
!= NULL
){
304 if( entry
->entry_state
== INGRESS_RESOLVED
){
305 if(!(entry
->refresh_time
))
306 entry
->refresh_time
= (2*(entry
->ctrl_info
.holding_time
))/3;
307 if( (now
.tv_sec
- entry
->reply_wait
.tv_sec
) > entry
->refresh_time
){
308 dprintk("mpoa: mpoa_caches.c: refreshing an entry.\n");
309 entry
->entry_state
= INGRESS_REFRESHING
;
315 read_unlock_bh(&client
->ingress_lock
);
318 static void in_destroy_cache(struct mpoa_client
*mpc
)
320 write_lock_irq(&mpc
->ingress_lock
);
321 while(mpc
->in_cache
!= NULL
)
322 mpc
->in_ops
->remove_entry(mpc
->in_cache
, mpc
);
323 write_unlock_irq(&mpc
->ingress_lock
);
328 static eg_cache_entry
*eg_cache_get_by_cache_id(uint32_t cache_id
, struct mpoa_client
*mpc
)
330 eg_cache_entry
*entry
;
332 read_lock_irq(&mpc
->egress_lock
);
333 entry
= mpc
->eg_cache
;
334 while(entry
!= NULL
){
335 if(entry
->ctrl_info
.cache_id
== cache_id
){
336 atomic_inc(&entry
->use
);
337 read_unlock_irq(&mpc
->egress_lock
);
342 read_unlock_irq(&mpc
->egress_lock
);
347 /* This can be called from any context since it saves CPU flags */
348 static eg_cache_entry
*eg_cache_get_by_tag(uint32_t tag
, struct mpoa_client
*mpc
)
351 eg_cache_entry
*entry
;
353 read_lock_irqsave(&mpc
->egress_lock
, flags
);
354 entry
= mpc
->eg_cache
;
355 while (entry
!= NULL
){
356 if (entry
->ctrl_info
.tag
== tag
) {
357 atomic_inc(&entry
->use
);
358 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
363 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
368 /* This can be called from any context since it saves CPU flags */
369 static eg_cache_entry
*eg_cache_get_by_vcc(struct atm_vcc
*vcc
, struct mpoa_client
*mpc
)
372 eg_cache_entry
*entry
;
374 read_lock_irqsave(&mpc
->egress_lock
, flags
);
375 entry
= mpc
->eg_cache
;
376 while (entry
!= NULL
){
377 if (entry
->shortcut
== vcc
) {
378 atomic_inc(&entry
->use
);
379 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
384 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
389 static eg_cache_entry
*eg_cache_get_by_src_ip(uint32_t ipaddr
, struct mpoa_client
*mpc
)
391 eg_cache_entry
*entry
;
393 read_lock_irq(&mpc
->egress_lock
);
394 entry
= mpc
->eg_cache
;
395 while(entry
!= NULL
){
396 if(entry
->latest_ip_addr
== ipaddr
) {
397 atomic_inc(&entry
->use
);
398 read_unlock_irq(&mpc
->egress_lock
);
403 read_unlock_irq(&mpc
->egress_lock
);
408 static void eg_cache_put(eg_cache_entry
*entry
)
410 if (atomic_dec_and_test(&entry
->use
)) {
411 memset(entry
, 0, sizeof(eg_cache_entry
));
419 * This should be called with write lock on
421 static void eg_cache_remove_entry(eg_cache_entry
*entry
,
422 struct mpoa_client
*client
)
425 struct k_message msg
;
427 vcc
= entry
->shortcut
;
428 dprintk("mpoa: mpoa_caches.c: removing an egress entry.\n");
429 if (entry
->prev
!= NULL
)
430 entry
->prev
->next
= entry
->next
;
432 client
->eg_cache
= entry
->next
;
433 if (entry
->next
!= NULL
)
434 entry
->next
->prev
= entry
->prev
;
435 client
->eg_ops
->put(entry
);
436 if(client
->in_cache
== NULL
&& client
->eg_cache
== NULL
){
437 msg
.type
= STOP_KEEP_ALIVE_SM
;
438 msg_to_mpoad(&msg
,client
);
441 /* Check if the ingress side still uses this VCC */
443 in_cache_entry
*in_entry
= client
->in_ops
->get_by_vcc(vcc
, client
);
444 if (in_entry
!= NULL
) {
445 client
->in_ops
->put(in_entry
);
448 vcc_release_async(vcc
, -EPIPE
);
454 static eg_cache_entry
*eg_cache_add_entry(struct k_message
*msg
, struct mpoa_client
*client
)
456 eg_cache_entry
*entry
= kmalloc(sizeof(eg_cache_entry
), GFP_KERNEL
);
459 printk("mpoa: mpoa_caches.c: new_eg_cache_entry: out of memory\n");
463 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
));
464 memset(entry
, 0, sizeof(eg_cache_entry
));
466 atomic_set(&entry
->use
, 1);
467 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry: about to lock\n");
468 write_lock_irq(&client
->egress_lock
);
469 entry
->next
= client
->eg_cache
;
471 if (client
->eg_cache
!= NULL
)
472 client
->eg_cache
->prev
= entry
;
473 client
->eg_cache
= entry
;
475 memcpy(entry
->MPS_ctrl_ATM_addr
, client
->mps_ctrl_addr
, ATM_ESA_LEN
);
476 entry
->ctrl_info
= msg
->content
.eg_info
;
477 do_gettimeofday(&(entry
->tv
));
478 entry
->entry_state
= EGRESS_RESOLVED
;
479 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry cache_id %lu\n", ntohl(entry
->ctrl_info
.cache_id
));
480 dprintk("mpoa: mpoa_caches.c: mps_ip = %u.%u.%u.%u\n",
481 NIPQUAD(entry
->ctrl_info
.mps_ip
));
482 atomic_inc(&entry
->use
);
484 write_unlock_irq(&client
->egress_lock
);
485 dprintk("mpoa: mpoa_caches.c: new_eg_cache_entry: unlocked\n");
490 static void update_eg_cache_entry(eg_cache_entry
* entry
, uint16_t holding_time
)
492 do_gettimeofday(&(entry
->tv
));
493 entry
->entry_state
= EGRESS_RESOLVED
;
494 entry
->ctrl_info
.holding_time
= holding_time
;
499 static void clear_expired(struct mpoa_client
*client
)
501 eg_cache_entry
*entry
, *next_entry
;
503 struct k_message msg
;
505 do_gettimeofday(&now
);
507 write_lock_irq(&client
->egress_lock
);
508 entry
= client
->eg_cache
;
509 while(entry
!= NULL
){
510 next_entry
= entry
->next
;
511 if((now
.tv_sec
- entry
->tv
.tv_sec
)
512 > entry
->ctrl_info
.holding_time
){
513 msg
.type
= SND_EGRESS_PURGE
;
514 msg
.content
.eg_info
= entry
->ctrl_info
;
515 dprintk("mpoa: mpoa_caches.c: egress_cache: holding time expired, cache_id = %lu.\n",ntohl(entry
->ctrl_info
.cache_id
));
516 msg_to_mpoad(&msg
, client
);
517 client
->eg_ops
->remove_entry(entry
, client
);
521 write_unlock_irq(&client
->egress_lock
);
526 static void eg_destroy_cache(struct mpoa_client
*mpc
)
528 write_lock_irq(&mpc
->egress_lock
);
529 while(mpc
->eg_cache
!= NULL
)
530 mpc
->eg_ops
->remove_entry(mpc
->eg_cache
, mpc
);
531 write_unlock_irq(&mpc
->egress_lock
);
538 static struct in_cache_ops ingress_ops
= {
539 in_cache_add_entry
, /* add_entry */
540 in_cache_get
, /* get */
541 in_cache_get_with_mask
, /* get_with_mask */
542 in_cache_get_by_vcc
, /* get_by_vcc */
543 in_cache_put
, /* put */
544 in_cache_remove_entry
, /* remove_entry */
545 cache_hit
, /* cache_hit */
546 clear_count_and_expired
, /* clear_count */
547 check_resolving_entries
, /* check_resolving */
548 refresh_entries
, /* refresh */
549 in_destroy_cache
/* destroy_cache */
552 static struct eg_cache_ops egress_ops
= {
553 eg_cache_add_entry
, /* add_entry */
554 eg_cache_get_by_cache_id
, /* get_by_cache_id */
555 eg_cache_get_by_tag
, /* get_by_tag */
556 eg_cache_get_by_vcc
, /* get_by_vcc */
557 eg_cache_get_by_src_ip
, /* get_by_src_ip */
558 eg_cache_put
, /* put */
559 eg_cache_remove_entry
, /* remove_entry */
560 update_eg_cache_entry
, /* update */
561 clear_expired
, /* clear_expired */
562 eg_destroy_cache
/* destroy_cache */
566 void atm_mpoa_init_cache(struct mpoa_client
*mpc
)
568 mpc
->in_ops
= &ingress_ops
;
569 mpc
->eg_ops
= &egress_ops
;