blob: 3286f9d527d3bc41cc180a313aa20036f03d7dec [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/types.h>
3#include <linux/atmmpc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/time.h>
6
7#include "mpoa_caches.h"
8#include "mpc.h"
9
10/*
11 * mpoa_caches.c: Implementation of ingress and egress cache
12 * handling functions
13 */
14
15#if 0
Joe Perchesb50c2ea2010-01-26 11:40:20 +000016#define dprintk(format, args...) \
17 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#else
Joe Perchesb50c2ea2010-01-26 11:40:20 +000019#define dprintk(format, args...) \
20 do { if (0) \
21 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
22 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#endif
24
25#if 0
Joe Perchesb50c2ea2010-01-26 11:40:20 +000026#define ddprintk(format, args...) \
27 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#else
Joe Perchesb50c2ea2010-01-26 11:40:20 +000029#define ddprintk(format, args...) \
30 do { if (0) \
31 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
32 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
Al Viro30d492d2006-11-14 21:11:29 -080035static in_cache_entry *in_cache_get(__be32 dst_ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct mpoa_client *client)
37{
38 in_cache_entry *entry;
39
40 read_lock_bh(&client->ingress_lock);
41 entry = client->in_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +000042 while (entry != NULL) {
43 if (entry->ctrl_info.in_dst_ip == dst_ip) {
Reshetova, Elena937149122017-07-04 15:53:03 +030044 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 read_unlock_bh(&client->ingress_lock);
46 return entry;
47 }
48 entry = entry->next;
49 }
50 read_unlock_bh(&client->ingress_lock);
51
52 return NULL;
53}
54
Al Viro30d492d2006-11-14 21:11:29 -080055static in_cache_entry *in_cache_get_with_mask(__be32 dst_ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct mpoa_client *client,
Al Viro30d492d2006-11-14 21:11:29 -080057 __be32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 in_cache_entry *entry;
60
61 read_lock_bh(&client->ingress_lock);
62 entry = client->in_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +000063 while (entry != NULL) {
64 if ((entry->ctrl_info.in_dst_ip & mask) == (dst_ip & mask)) {
Reshetova, Elena937149122017-07-04 15:53:03 +030065 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 read_unlock_bh(&client->ingress_lock);
67 return entry;
68 }
69 entry = entry->next;
70 }
71 read_unlock_bh(&client->ingress_lock);
72
73 return NULL;
74
75}
76
77static in_cache_entry *in_cache_get_by_vcc(struct atm_vcc *vcc,
Joe Perchesbee67d32010-01-26 11:40:10 +000078 struct mpoa_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 in_cache_entry *entry;
81
82 read_lock_bh(&client->ingress_lock);
83 entry = client->in_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +000084 while (entry != NULL) {
85 if (entry->shortcut == vcc) {
Reshetova, Elena937149122017-07-04 15:53:03 +030086 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 read_unlock_bh(&client->ingress_lock);
88 return entry;
89 }
90 entry = entry->next;
91 }
92 read_unlock_bh(&client->ingress_lock);
93
94 return NULL;
95}
96
Al Viro30d492d2006-11-14 21:11:29 -080097static in_cache_entry *in_cache_add_entry(__be32 dst_ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct mpoa_client *client)
99{
Arnaldo Carvalho de Melo2afe37c2006-11-21 01:14:33 -0200100 in_cache_entry *entry = kzalloc(sizeof(in_cache_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (entry == NULL) {
Joe Perchesbee67d32010-01-26 11:40:10 +0000103 pr_info("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return NULL;
105 }
106
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000107 dprintk("adding an ingress entry, ip = %pI4\n", &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Reshetova, Elena937149122017-07-04 15:53:03 +0300109 refcount_set(&entry->use, 1);
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000110 dprintk("new_in_cache_entry: about to lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 write_lock_bh(&client->ingress_lock);
112 entry->next = client->in_cache;
113 entry->prev = NULL;
114 if (client->in_cache != NULL)
115 client->in_cache->prev = entry;
116 client->in_cache = entry;
117
118 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
119 entry->ctrl_info.in_dst_ip = dst_ip;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100120 entry->time = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 entry->retry_time = client->parameters.mpc_p4;
122 entry->count = 1;
123 entry->entry_state = INGRESS_INVALID;
124 entry->ctrl_info.holding_time = HOLDING_TIME_DEFAULT;
Reshetova, Elena937149122017-07-04 15:53:03 +0300125 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 write_unlock_bh(&client->ingress_lock);
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000128 dprintk("new_in_cache_entry: unlocked\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 return entry;
131}
132
133static int cache_hit(in_cache_entry *entry, struct mpoa_client *mpc)
134{
135 struct atm_mpoa_qos *qos;
136 struct k_message msg;
137
138 entry->count++;
Joe Perchesbee67d32010-01-26 11:40:10 +0000139 if (entry->entry_state == INGRESS_RESOLVED && entry->shortcut != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return OPEN;
141
Joe Perchesbee67d32010-01-26 11:40:10 +0000142 if (entry->entry_state == INGRESS_REFRESHING) {
143 if (entry->count > mpc->parameters.mpc_p1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 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);
Joe Perchesbee67d32010-01-26 11:40:10 +0000148 if (qos != NULL)
149 msg.qos = qos->qos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 msg_to_mpoad(&msg, mpc);
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100151 entry->reply_wait = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 entry->entry_state = INGRESS_RESOLVING;
153 }
Joe Perchesbee67d32010-01-26 11:40:10 +0000154 if (entry->shortcut != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return OPEN;
156 return CLOSED;
157 }
158
Joe Perchesbee67d32010-01-26 11:40:10 +0000159 if (entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return OPEN;
161
Joe Perchesbee67d32010-01-26 11:40:10 +0000162 if (entry->count > mpc->parameters.mpc_p1 &&
163 entry->entry_state == INGRESS_INVALID) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000164 dprintk("(%s) threshold exceeded for ip %pI4, sending MPOA res req\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -0700165 mpc->dev->name, &entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 entry->entry_state = INGRESS_RESOLVING;
Joe Perchesbee67d32010-01-26 11:40:10 +0000167 msg.type = SND_MPOA_RES_RQST;
168 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 msg.content.in_info = entry->ctrl_info;
170 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
Joe Perchesbee67d32010-01-26 11:40:10 +0000171 if (qos != NULL)
172 msg.qos = qos->qos;
173 msg_to_mpoad(&msg, mpc);
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100174 entry->reply_wait = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
177 return CLOSED;
178}
179
180static void in_cache_put(in_cache_entry *entry)
181{
Reshetova, Elena937149122017-07-04 15:53:03 +0300182 if (refcount_dec_and_test(&entry->use)) {
zhong jiangda3a3b62019-09-04 10:39:12 +0800183 kzfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This should be called with write lock on
189 */
190static void in_cache_remove_entry(in_cache_entry *entry,
191 struct mpoa_client *client)
192{
193 struct atm_vcc *vcc;
194 struct k_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 vcc = entry->shortcut;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000197 dprintk("removing an ingress entry, ip = %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -0700198 &entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (entry->prev != NULL)
201 entry->prev->next = entry->next;
202 else
203 client->in_cache = entry->next;
204 if (entry->next != NULL)
205 entry->next->prev = entry->prev;
206 client->in_ops->put(entry);
Joe Perchesbee67d32010-01-26 11:40:10 +0000207 if (client->in_cache == NULL && client->eg_cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 msg.type = STOP_KEEP_ALIVE_SM;
Joe Perchesbee67d32010-01-26 11:40:10 +0000209 msg_to_mpoad(&msg, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 /* Check if the egress side still uses this VCC */
213 if (vcc != NULL) {
Joe Perchesbee67d32010-01-26 11:40:10 +0000214 eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc,
215 client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (eg_entry != NULL) {
217 client->eg_ops->put(eg_entry);
218 return;
219 }
220 vcc_release_async(vcc, -EPIPE);
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/* Call this every MPC-p2 seconds... Not exactly correct solution,
225 but an easy one... */
226static void clear_count_and_expired(struct mpoa_client *client)
227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 in_cache_entry *entry, *next_entry;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100229 time64_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100231 now = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 write_lock_bh(&client->ingress_lock);
234 entry = client->in_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000235 while (entry != NULL) {
236 entry->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 next_entry = entry->next;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100238 if ((now - entry->time) > entry->ctrl_info.holding_time) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000239 dprintk("holding time expired, ip = %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -0700240 &entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 client->in_ops->remove_entry(entry, client);
242 }
243 entry = next_entry;
244 }
245 write_unlock_bh(&client->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/* Call this every MPC-p4 seconds. */
249static void check_resolving_entries(struct mpoa_client *client)
250{
251
252 struct atm_mpoa_qos *qos;
253 in_cache_entry *entry;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100254 time64_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct k_message msg;
256
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100257 now = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 read_lock_bh(&client->ingress_lock);
260 entry = client->in_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000261 while (entry != NULL) {
262 if (entry->entry_state == INGRESS_RESOLVING) {
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100263
264 if ((now - entry->hold_down)
265 < client->parameters.mpc_p6) {
Joe Perchesbee67d32010-01-26 11:40:10 +0000266 entry = entry->next; /* Entry in hold down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 continue;
268 }
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100269 if ((now - entry->reply_wait) > entry->retry_time) {
Joe Perchesbee67d32010-01-26 11:40:10 +0000270 entry->retry_time = MPC_C1 * (entry->retry_time);
271 /*
272 * Retry time maximum exceeded,
273 * put entry in hold down.
274 */
275 if (entry->retry_time > client->parameters.mpc_p5) {
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100276 entry->hold_down = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 entry->retry_time = client->parameters.mpc_p4;
278 entry = entry->next;
279 continue;
280 }
281 /* Ask daemon to send a resolution request. */
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100282 memset(&entry->hold_down, 0, sizeof(time64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 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);
Joe Perchesbee67d32010-01-26 11:40:10 +0000287 if (qos != NULL)
288 msg.qos = qos->qos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 msg_to_mpoad(&msg, client);
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100290 entry->reply_wait = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 }
293 entry = entry->next;
294 }
295 read_unlock_bh(&client->ingress_lock);
296}
297
298/* Call this every MPC-p5 seconds. */
299static void refresh_entries(struct mpoa_client *client)
300{
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100301 time64_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct in_cache_entry *entry = client->in_cache;
303
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000304 ddprintk("refresh_entries\n");
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100305 now = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 read_lock_bh(&client->ingress_lock);
Joe Perchesbee67d32010-01-26 11:40:10 +0000308 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;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100312 if ((now - entry->reply_wait) >
Joe Perchesbee67d32010-01-26 11:40:10 +0000313 entry->refresh_time) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000314 dprintk("refreshing an entry.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 entry->entry_state = INGRESS_REFRESHING;
316
317 }
318 }
319 entry = entry->next;
320 }
321 read_unlock_bh(&client->ingress_lock);
322}
323
324static void in_destroy_cache(struct mpoa_client *mpc)
325{
326 write_lock_irq(&mpc->ingress_lock);
Joe Perchesbee67d32010-01-26 11:40:10 +0000327 while (mpc->in_cache != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 mpc->in_ops->remove_entry(mpc->in_cache, mpc);
329 write_unlock_irq(&mpc->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Joe Perchesbee67d32010-01-26 11:40:10 +0000332static eg_cache_entry *eg_cache_get_by_cache_id(__be32 cache_id,
333 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 eg_cache_entry *entry;
336
337 read_lock_irq(&mpc->egress_lock);
338 entry = mpc->eg_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000339 while (entry != NULL) {
340 if (entry->ctrl_info.cache_id == cache_id) {
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300341 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 read_unlock_irq(&mpc->egress_lock);
343 return entry;
344 }
345 entry = entry->next;
346 }
347 read_unlock_irq(&mpc->egress_lock);
348
349 return NULL;
350}
351
352/* This can be called from any context since it saves CPU flags */
Al Viro30d492d2006-11-14 21:11:29 -0800353static eg_cache_entry *eg_cache_get_by_tag(__be32 tag, struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 unsigned long flags;
356 eg_cache_entry *entry;
357
358 read_lock_irqsave(&mpc->egress_lock, flags);
359 entry = mpc->eg_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000360 while (entry != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (entry->ctrl_info.tag == tag) {
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300362 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 read_unlock_irqrestore(&mpc->egress_lock, flags);
364 return entry;
365 }
366 entry = entry->next;
367 }
368 read_unlock_irqrestore(&mpc->egress_lock, flags);
369
370 return NULL;
371}
372
373/* This can be called from any context since it saves CPU flags */
Joe Perchesbee67d32010-01-26 11:40:10 +0000374static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc,
375 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 unsigned long flags;
378 eg_cache_entry *entry;
379
380 read_lock_irqsave(&mpc->egress_lock, flags);
381 entry = mpc->eg_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000382 while (entry != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (entry->shortcut == vcc) {
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300384 refcount_inc(&entry->use);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900385 read_unlock_irqrestore(&mpc->egress_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return entry;
387 }
388 entry = entry->next;
389 }
390 read_unlock_irqrestore(&mpc->egress_lock, flags);
391
392 return NULL;
393}
394
Joe Perchesbee67d32010-01-26 11:40:10 +0000395static eg_cache_entry *eg_cache_get_by_src_ip(__be32 ipaddr,
396 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 eg_cache_entry *entry;
399
400 read_lock_irq(&mpc->egress_lock);
401 entry = mpc->eg_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000402 while (entry != NULL) {
403 if (entry->latest_ip_addr == ipaddr) {
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300404 refcount_inc(&entry->use);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900405 read_unlock_irq(&mpc->egress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return entry;
407 }
408 entry = entry->next;
409 }
410 read_unlock_irq(&mpc->egress_lock);
411
412 return NULL;
413}
414
415static void eg_cache_put(eg_cache_entry *entry)
416{
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300417 if (refcount_dec_and_test(&entry->use)) {
zhong jiangda3a3b62019-09-04 10:39:12 +0800418 kzfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/*
423 * This should be called with write lock on
424 */
425static void eg_cache_remove_entry(eg_cache_entry *entry,
426 struct mpoa_client *client)
427{
428 struct atm_vcc *vcc;
429 struct k_message msg;
430
431 vcc = entry->shortcut;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000432 dprintk("removing an egress entry.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (entry->prev != NULL)
434 entry->prev->next = entry->next;
435 else
436 client->eg_cache = entry->next;
437 if (entry->next != NULL)
438 entry->next->prev = entry->prev;
439 client->eg_ops->put(entry);
Joe Perchesbee67d32010-01-26 11:40:10 +0000440 if (client->in_cache == NULL && client->eg_cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 msg.type = STOP_KEEP_ALIVE_SM;
Joe Perchesbee67d32010-01-26 11:40:10 +0000442 msg_to_mpoad(&msg, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 /* Check if the ingress side still uses this VCC */
446 if (vcc != NULL) {
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);
450 return;
451 }
452 vcc_release_async(vcc, -EPIPE);
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Joe Perchesbee67d32010-01-26 11:40:10 +0000456static eg_cache_entry *eg_cache_add_entry(struct k_message *msg,
457 struct mpoa_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Arnaldo Carvalho de Melo2afe37c2006-11-21 01:14:33 -0200459 eg_cache_entry *entry = kzalloc(sizeof(eg_cache_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (entry == NULL) {
Joe Perchesbee67d32010-01-26 11:40:10 +0000462 pr_info("out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return NULL;
464 }
465
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000466 dprintk("adding an egress entry, ip = %pI4, this should be our IP\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -0700467 &msg->content.eg_info.eg_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300469 refcount_set(&entry->use, 1);
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000470 dprintk("new_eg_cache_entry: about to lock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 write_lock_irq(&client->egress_lock);
472 entry->next = client->eg_cache;
473 entry->prev = NULL;
474 if (client->eg_cache != NULL)
475 client->eg_cache->prev = entry;
476 client->eg_cache = entry;
477
478 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
479 entry->ctrl_info = msg->content.eg_info;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100480 entry->time = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 entry->entry_state = EGRESS_RESOLVED;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000482 dprintk("new_eg_cache_entry cache_id %u\n",
Joe Perchesbee67d32010-01-26 11:40:10 +0000483 ntohl(entry->ctrl_info.cache_id));
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000484 dprintk("mps_ip = %pI4\n", &entry->ctrl_info.mps_ip);
Reshetova, Elenae00bdbe2017-07-04 15:53:04 +0300485 refcount_inc(&entry->use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 write_unlock_irq(&client->egress_lock);
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000488 dprintk("new_eg_cache_entry: unlocked\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 return entry;
491}
492
Joe Perchesbee67d32010-01-26 11:40:10 +0000493static void update_eg_cache_entry(eg_cache_entry *entry, uint16_t holding_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100495 entry->time = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 entry->entry_state = EGRESS_RESOLVED;
497 entry->ctrl_info.holding_time = holding_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500static void clear_expired(struct mpoa_client *client)
501{
502 eg_cache_entry *entry, *next_entry;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100503 time64_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct k_message msg;
505
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100506 now = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 write_lock_irq(&client->egress_lock);
509 entry = client->eg_cache;
Joe Perchesbee67d32010-01-26 11:40:10 +0000510 while (entry != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 next_entry = entry->next;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100512 if ((now - entry->time) > entry->ctrl_info.holding_time) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 msg.type = SND_EGRESS_PURGE;
514 msg.content.eg_info = entry->ctrl_info;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000515 dprintk("egress_cache: holding time expired, cache_id = %u.\n",
Joe Perchesbee67d32010-01-26 11:40:10 +0000516 ntohl(entry->ctrl_info.cache_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 msg_to_mpoad(&msg, client);
518 client->eg_ops->remove_entry(entry, client);
519 }
520 entry = next_entry;
521 }
522 write_unlock_irq(&client->egress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525static void eg_destroy_cache(struct mpoa_client *mpc)
526{
527 write_lock_irq(&mpc->egress_lock);
Joe Perchesbee67d32010-01-26 11:40:10 +0000528 while (mpc->eg_cache != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 mpc->eg_ops->remove_entry(mpc->eg_cache, mpc);
530 write_unlock_irq(&mpc->egress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533
Julia Lawall4dd191b2015-11-21 18:28:05 +0100534static const struct in_cache_ops ingress_ops = {
Kees Cook99a5e172016-12-16 16:58:43 -0800535 .add_entry = in_cache_add_entry,
536 .get = in_cache_get,
537 .get_with_mask = in_cache_get_with_mask,
538 .get_by_vcc = in_cache_get_by_vcc,
539 .put = in_cache_put,
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546};
547
Julia Lawall4dd191b2015-11-21 18:28:05 +0100548static const struct eg_cache_ops egress_ops = {
Kees Cook99a5e172016-12-16 16:58:43 -0800549 .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,
554 .put = eg_cache_put,
555 .remove_entry = eg_cache_remove_entry,
556 .update = update_eg_cache_entry,
557 .clear_expired = clear_expired,
558 .destroy_cache = eg_destroy_cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561void atm_mpoa_init_cache(struct mpoa_client *mpc)
562{
563 mpc->in_ops = &ingress_ops;
564 mpc->eg_ops = &egress_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}