1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/atmmpc.h>
4 #include <linux/slab.h>
5 #include <linux/time.h>
7 #include "mpoa_caches.h"
11 * mpoa_caches.c: Implementation of ingress and egress cache
16 #define dprintk(format, args...) \
17 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
19 #define dprintk(format, args...) \
21 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
26 #define ddprintk(format, args...) \
27 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
29 #define ddprintk(format, args...) \
31 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
35 static in_cache_entry
*in_cache_get(__be32 dst_ip
,
36 struct mpoa_client
*client
)
38 in_cache_entry
*entry
;
40 read_lock_bh(&client
->ingress_lock
);
41 entry
= client
->in_cache
;
42 while (entry
!= NULL
) {
43 if (entry
->ctrl_info
.in_dst_ip
== dst_ip
) {
44 refcount_inc(&entry
->use
);
45 read_unlock_bh(&client
->ingress_lock
);
50 read_unlock_bh(&client
->ingress_lock
);
55 static in_cache_entry
*in_cache_get_with_mask(__be32 dst_ip
,
56 struct mpoa_client
*client
,
59 in_cache_entry
*entry
;
61 read_lock_bh(&client
->ingress_lock
);
62 entry
= client
->in_cache
;
63 while (entry
!= NULL
) {
64 if ((entry
->ctrl_info
.in_dst_ip
& mask
) == (dst_ip
& mask
)) {
65 refcount_inc(&entry
->use
);
66 read_unlock_bh(&client
->ingress_lock
);
71 read_unlock_bh(&client
->ingress_lock
);
77 static in_cache_entry
*in_cache_get_by_vcc(struct atm_vcc
*vcc
,
78 struct mpoa_client
*client
)
80 in_cache_entry
*entry
;
82 read_lock_bh(&client
->ingress_lock
);
83 entry
= client
->in_cache
;
84 while (entry
!= NULL
) {
85 if (entry
->shortcut
== vcc
) {
86 refcount_inc(&entry
->use
);
87 read_unlock_bh(&client
->ingress_lock
);
92 read_unlock_bh(&client
->ingress_lock
);
97 static in_cache_entry
*in_cache_add_entry(__be32 dst_ip
,
98 struct mpoa_client
*client
)
100 in_cache_entry
*entry
= kzalloc(sizeof(in_cache_entry
), GFP_KERNEL
);
103 pr_info("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n");
107 dprintk("adding an ingress entry, ip = %pI4\n", &dst_ip
);
109 refcount_set(&entry
->use
, 1);
110 dprintk("new_in_cache_entry: about to lock\n");
111 write_lock_bh(&client
->ingress_lock
);
112 entry
->next
= client
->in_cache
;
114 if (client
->in_cache
!= NULL
)
115 client
->in_cache
->prev
= entry
;
116 client
->in_cache
= entry
;
118 memcpy(entry
->MPS_ctrl_ATM_addr
, client
->mps_ctrl_addr
, ATM_ESA_LEN
);
119 entry
->ctrl_info
.in_dst_ip
= dst_ip
;
120 entry
->time
= ktime_get_seconds();
121 entry
->retry_time
= client
->parameters
.mpc_p4
;
123 entry
->entry_state
= INGRESS_INVALID
;
124 entry
->ctrl_info
.holding_time
= HOLDING_TIME_DEFAULT
;
125 refcount_inc(&entry
->use
);
127 write_unlock_bh(&client
->ingress_lock
);
128 dprintk("new_in_cache_entry: unlocked\n");
133 static int cache_hit(in_cache_entry
*entry
, struct mpoa_client
*mpc
)
135 struct atm_mpoa_qos
*qos
;
136 struct k_message msg
;
139 if (entry
->entry_state
== INGRESS_RESOLVED
&& entry
->shortcut
!= NULL
)
142 if (entry
->entry_state
== INGRESS_REFRESHING
) {
143 if (entry
->count
> mpc
->parameters
.mpc_p1
) {
144 msg
.type
= SND_MPOA_RES_RQST
;
145 msg
.content
.in_info
= entry
->ctrl_info
;
146 memcpy(msg
.MPS_ctrl
, mpc
->mps_ctrl_addr
, ATM_ESA_LEN
);
147 qos
= atm_mpoa_search_qos(entry
->ctrl_info
.in_dst_ip
);
150 msg_to_mpoad(&msg
, mpc
);
151 entry
->reply_wait
= ktime_get_seconds();
152 entry
->entry_state
= INGRESS_RESOLVING
;
154 if (entry
->shortcut
!= NULL
)
159 if (entry
->entry_state
== INGRESS_RESOLVING
&& entry
->shortcut
!= NULL
)
162 if (entry
->count
> mpc
->parameters
.mpc_p1
&&
163 entry
->entry_state
== INGRESS_INVALID
) {
164 dprintk("(%s) threshold exceeded for ip %pI4, sending MPOA res req\n",
165 mpc
->dev
->name
, &entry
->ctrl_info
.in_dst_ip
);
166 entry
->entry_state
= INGRESS_RESOLVING
;
167 msg
.type
= SND_MPOA_RES_RQST
;
168 memcpy(msg
.MPS_ctrl
, mpc
->mps_ctrl_addr
, ATM_ESA_LEN
);
169 msg
.content
.in_info
= entry
->ctrl_info
;
170 qos
= atm_mpoa_search_qos(entry
->ctrl_info
.in_dst_ip
);
173 msg_to_mpoad(&msg
, mpc
);
174 entry
->reply_wait
= ktime_get_seconds();
180 static void in_cache_put(in_cache_entry
*entry
)
182 if (refcount_dec_and_test(&entry
->use
)) {
183 kfree_sensitive(entry
);
188 * This should be called with write lock on
190 static void in_cache_remove_entry(in_cache_entry
*entry
,
191 struct mpoa_client
*client
)
194 struct k_message msg
;
196 vcc
= entry
->shortcut
;
197 dprintk("removing an ingress entry, ip = %pI4\n",
198 &entry
->ctrl_info
.in_dst_ip
);
200 if (entry
->prev
!= NULL
)
201 entry
->prev
->next
= entry
->next
;
203 client
->in_cache
= entry
->next
;
204 if (entry
->next
!= NULL
)
205 entry
->next
->prev
= entry
->prev
;
206 client
->in_ops
->put(entry
);
207 if (client
->in_cache
== NULL
&& client
->eg_cache
== NULL
) {
208 msg
.type
= STOP_KEEP_ALIVE_SM
;
209 msg_to_mpoad(&msg
, client
);
212 /* Check if the egress side still uses this VCC */
214 eg_cache_entry
*eg_entry
= client
->eg_ops
->get_by_vcc(vcc
,
216 if (eg_entry
!= NULL
) {
217 client
->eg_ops
->put(eg_entry
);
220 vcc_release_async(vcc
, -EPIPE
);
224 /* Call this every MPC-p2 seconds... Not exactly correct solution,
225 but an easy one... */
226 static void clear_count_and_expired(struct mpoa_client
*client
)
228 in_cache_entry
*entry
, *next_entry
;
231 now
= ktime_get_seconds();
233 write_lock_bh(&client
->ingress_lock
);
234 entry
= client
->in_cache
;
235 while (entry
!= NULL
) {
237 next_entry
= entry
->next
;
238 if ((now
- entry
->time
) > entry
->ctrl_info
.holding_time
) {
239 dprintk("holding time expired, ip = %pI4\n",
240 &entry
->ctrl_info
.in_dst_ip
);
241 client
->in_ops
->remove_entry(entry
, client
);
245 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 now
= ktime_get_seconds();
259 read_lock_bh(&client
->ingress_lock
);
260 entry
= client
->in_cache
;
261 while (entry
!= NULL
) {
262 if (entry
->entry_state
== INGRESS_RESOLVING
) {
264 if ((now
- entry
->hold_down
)
265 < client
->parameters
.mpc_p6
) {
266 entry
= entry
->next
; /* Entry in hold down */
269 if ((now
- entry
->reply_wait
) > entry
->retry_time
) {
270 entry
->retry_time
= MPC_C1
* (entry
->retry_time
);
272 * Retry time maximum exceeded,
273 * put entry in hold down.
275 if (entry
->retry_time
> client
->parameters
.mpc_p5
) {
276 entry
->hold_down
= ktime_get_seconds();
277 entry
->retry_time
= client
->parameters
.mpc_p4
;
281 /* Ask daemon to send a resolution request. */
282 memset(&entry
->hold_down
, 0, sizeof(time64_t
));
283 msg
.type
= SND_MPOA_RES_RTRY
;
284 memcpy(msg
.MPS_ctrl
, client
->mps_ctrl_addr
, ATM_ESA_LEN
);
285 msg
.content
.in_info
= entry
->ctrl_info
;
286 qos
= atm_mpoa_search_qos(entry
->ctrl_info
.in_dst_ip
);
289 msg_to_mpoad(&msg
, client
);
290 entry
->reply_wait
= ktime_get_seconds();
295 read_unlock_bh(&client
->ingress_lock
);
298 /* Call this every MPC-p5 seconds. */
299 static void refresh_entries(struct mpoa_client
*client
)
302 struct in_cache_entry
*entry
= client
->in_cache
;
304 ddprintk("refresh_entries\n");
305 now
= ktime_get_seconds();
307 read_lock_bh(&client
->ingress_lock
);
308 while (entry
!= NULL
) {
309 if (entry
->entry_state
== INGRESS_RESOLVED
) {
310 if (!(entry
->refresh_time
))
311 entry
->refresh_time
= (2 * (entry
->ctrl_info
.holding_time
))/3;
312 if ((now
- entry
->reply_wait
) >
313 entry
->refresh_time
) {
314 dprintk("refreshing an entry.\n");
315 entry
->entry_state
= INGRESS_REFRESHING
;
321 read_unlock_bh(&client
->ingress_lock
);
324 static void in_destroy_cache(struct mpoa_client
*mpc
)
326 write_lock_irq(&mpc
->ingress_lock
);
327 while (mpc
->in_cache
!= NULL
)
328 mpc
->in_ops
->remove_entry(mpc
->in_cache
, mpc
);
329 write_unlock_irq(&mpc
->ingress_lock
);
332 static eg_cache_entry
*eg_cache_get_by_cache_id(__be32 cache_id
,
333 struct mpoa_client
*mpc
)
335 eg_cache_entry
*entry
;
337 read_lock_irq(&mpc
->egress_lock
);
338 entry
= mpc
->eg_cache
;
339 while (entry
!= NULL
) {
340 if (entry
->ctrl_info
.cache_id
== cache_id
) {
341 refcount_inc(&entry
->use
);
342 read_unlock_irq(&mpc
->egress_lock
);
347 read_unlock_irq(&mpc
->egress_lock
);
352 /* This can be called from any context since it saves CPU flags */
353 static eg_cache_entry
*eg_cache_get_by_tag(__be32 tag
, struct mpoa_client
*mpc
)
356 eg_cache_entry
*entry
;
358 read_lock_irqsave(&mpc
->egress_lock
, flags
);
359 entry
= mpc
->eg_cache
;
360 while (entry
!= NULL
) {
361 if (entry
->ctrl_info
.tag
== tag
) {
362 refcount_inc(&entry
->use
);
363 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
368 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
373 /* This can be called from any context since it saves CPU flags */
374 static eg_cache_entry
*eg_cache_get_by_vcc(struct atm_vcc
*vcc
,
375 struct mpoa_client
*mpc
)
378 eg_cache_entry
*entry
;
380 read_lock_irqsave(&mpc
->egress_lock
, flags
);
381 entry
= mpc
->eg_cache
;
382 while (entry
!= NULL
) {
383 if (entry
->shortcut
== vcc
) {
384 refcount_inc(&entry
->use
);
385 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
390 read_unlock_irqrestore(&mpc
->egress_lock
, flags
);
395 static eg_cache_entry
*eg_cache_get_by_src_ip(__be32 ipaddr
,
396 struct mpoa_client
*mpc
)
398 eg_cache_entry
*entry
;
400 read_lock_irq(&mpc
->egress_lock
);
401 entry
= mpc
->eg_cache
;
402 while (entry
!= NULL
) {
403 if (entry
->latest_ip_addr
== ipaddr
) {
404 refcount_inc(&entry
->use
);
405 read_unlock_irq(&mpc
->egress_lock
);
410 read_unlock_irq(&mpc
->egress_lock
);
415 static void eg_cache_put(eg_cache_entry
*entry
)
417 if (refcount_dec_and_test(&entry
->use
)) {
418 kfree_sensitive(entry
);
423 * This should be called with write lock on
425 static void eg_cache_remove_entry(eg_cache_entry
*entry
,
426 struct mpoa_client
*client
)
429 struct k_message msg
;
431 vcc
= entry
->shortcut
;
432 dprintk("removing an egress entry.\n");
433 if (entry
->prev
!= NULL
)
434 entry
->prev
->next
= entry
->next
;
436 client
->eg_cache
= entry
->next
;
437 if (entry
->next
!= NULL
)
438 entry
->next
->prev
= entry
->prev
;
439 client
->eg_ops
->put(entry
);
440 if (client
->in_cache
== NULL
&& client
->eg_cache
== NULL
) {
441 msg
.type
= STOP_KEEP_ALIVE_SM
;
442 msg_to_mpoad(&msg
, client
);
445 /* Check if the ingress side still uses this VCC */
447 in_cache_entry
*in_entry
= client
->in_ops
->get_by_vcc(vcc
, client
);
448 if (in_entry
!= NULL
) {
449 client
->in_ops
->put(in_entry
);
452 vcc_release_async(vcc
, -EPIPE
);
456 static eg_cache_entry
*eg_cache_add_entry(struct k_message
*msg
,
457 struct mpoa_client
*client
)
459 eg_cache_entry
*entry
= kzalloc(sizeof(eg_cache_entry
), GFP_KERNEL
);
462 pr_info("out of memory\n");
466 dprintk("adding an egress entry, ip = %pI4, this should be our IP\n",
467 &msg
->content
.eg_info
.eg_dst_ip
);
469 refcount_set(&entry
->use
, 1);
470 dprintk("new_eg_cache_entry: about to lock\n");
471 write_lock_irq(&client
->egress_lock
);
472 entry
->next
= client
->eg_cache
;
474 if (client
->eg_cache
!= NULL
)
475 client
->eg_cache
->prev
= entry
;
476 client
->eg_cache
= entry
;
478 memcpy(entry
->MPS_ctrl_ATM_addr
, client
->mps_ctrl_addr
, ATM_ESA_LEN
);
479 entry
->ctrl_info
= msg
->content
.eg_info
;
480 entry
->time
= ktime_get_seconds();
481 entry
->entry_state
= EGRESS_RESOLVED
;
482 dprintk("new_eg_cache_entry cache_id %u\n",
483 ntohl(entry
->ctrl_info
.cache_id
));
484 dprintk("mps_ip = %pI4\n", &entry
->ctrl_info
.mps_ip
);
485 refcount_inc(&entry
->use
);
487 write_unlock_irq(&client
->egress_lock
);
488 dprintk("new_eg_cache_entry: unlocked\n");
493 static void update_eg_cache_entry(eg_cache_entry
*entry
, uint16_t holding_time
)
495 entry
->time
= ktime_get_seconds();
496 entry
->entry_state
= EGRESS_RESOLVED
;
497 entry
->ctrl_info
.holding_time
= holding_time
;
500 static void clear_expired(struct mpoa_client
*client
)
502 eg_cache_entry
*entry
, *next_entry
;
504 struct k_message msg
;
506 now
= ktime_get_seconds();
508 write_lock_irq(&client
->egress_lock
);
509 entry
= client
->eg_cache
;
510 while (entry
!= NULL
) {
511 next_entry
= entry
->next
;
512 if ((now
- entry
->time
) > entry
->ctrl_info
.holding_time
) {
513 msg
.type
= SND_EGRESS_PURGE
;
514 msg
.content
.eg_info
= entry
->ctrl_info
;
515 dprintk("egress_cache: holding time expired, cache_id = %u.\n",
516 ntohl(entry
->ctrl_info
.cache_id
));
517 msg_to_mpoad(&msg
, client
);
518 client
->eg_ops
->remove_entry(entry
, client
);
522 write_unlock_irq(&client
->egress_lock
);
525 static void eg_destroy_cache(struct mpoa_client
*mpc
)
527 write_lock_irq(&mpc
->egress_lock
);
528 while (mpc
->eg_cache
!= NULL
)
529 mpc
->eg_ops
->remove_entry(mpc
->eg_cache
, mpc
);
530 write_unlock_irq(&mpc
->egress_lock
);
534 static const struct in_cache_ops ingress_ops
= {
535 .add_entry
= in_cache_add_entry
,
537 .get_with_mask
= in_cache_get_with_mask
,
538 .get_by_vcc
= in_cache_get_by_vcc
,
540 .remove_entry
= in_cache_remove_entry
,
541 .cache_hit
= cache_hit
,
542 .clear_count
= clear_count_and_expired
,
543 .check_resolving
= check_resolving_entries
,
544 .refresh
= refresh_entries
,
545 .destroy_cache
= in_destroy_cache
548 static const struct eg_cache_ops egress_ops
= {
549 .add_entry
= eg_cache_add_entry
,
550 .get_by_cache_id
= eg_cache_get_by_cache_id
,
551 .get_by_tag
= eg_cache_get_by_tag
,
552 .get_by_vcc
= eg_cache_get_by_vcc
,
553 .get_by_src_ip
= eg_cache_get_by_src_ip
,
555 .remove_entry
= eg_cache_remove_entry
,
556 .update
= update_eg_cache_entry
,
557 .clear_expired
= clear_expired
,
558 .destroy_cache
= eg_destroy_cache
561 void atm_mpoa_init_cache(struct mpoa_client
*mpc
)
563 mpc
->in_ops
= &ingress_ops
;
564 mpc
->eg_ops
= &egress_ops
;