mpoa_caches.c 14 KB

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