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