mpoa_caches.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. kfree_sensitive(entry);
  156. }
  157. }
  158. /*
  159. * This should be called with write lock on
  160. */
  161. static void in_cache_remove_entry(in_cache_entry *entry,
  162. struct mpoa_client *client)
  163. {
  164. struct atm_vcc *vcc;
  165. struct k_message msg;
  166. vcc = entry->shortcut;
  167. dprintk("removing an ingress entry, ip = %pI4\n",
  168. &entry->ctrl_info.in_dst_ip);
  169. if (entry->prev != NULL)
  170. entry->prev->next = entry->next;
  171. else
  172. client->in_cache = entry->next;
  173. if (entry->next != NULL)
  174. entry->next->prev = entry->prev;
  175. client->in_ops->put(entry);
  176. if (client->in_cache == NULL && client->eg_cache == NULL) {
  177. msg.type = STOP_KEEP_ALIVE_SM;
  178. msg_to_mpoad(&msg, client);
  179. }
  180. /* Check if the egress side still uses this VCC */
  181. if (vcc != NULL) {
  182. eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc,
  183. client);
  184. if (eg_entry != NULL) {
  185. client->eg_ops->put(eg_entry);
  186. return;
  187. }
  188. vcc_release_async(vcc, -EPIPE);
  189. }
  190. }
  191. /* Call this every MPC-p2 seconds... Not exactly correct solution,
  192. but an easy one... */
  193. static void clear_count_and_expired(struct mpoa_client *client)
  194. {
  195. in_cache_entry *entry, *next_entry;
  196. time64_t now;
  197. now = ktime_get_seconds();
  198. write_lock_bh(&client->ingress_lock);
  199. entry = client->in_cache;
  200. while (entry != NULL) {
  201. entry->count = 0;
  202. next_entry = entry->next;
  203. if ((now - entry->time) > entry->ctrl_info.holding_time) {
  204. dprintk("holding time expired, ip = %pI4\n",
  205. &entry->ctrl_info.in_dst_ip);
  206. client->in_ops->remove_entry(entry, client);
  207. }
  208. entry = next_entry;
  209. }
  210. write_unlock_bh(&client->ingress_lock);
  211. }
  212. /* Call this every MPC-p4 seconds. */
  213. static void check_resolving_entries(struct mpoa_client *client)
  214. {
  215. struct atm_mpoa_qos *qos;
  216. in_cache_entry *entry;
  217. time64_t now;
  218. struct k_message msg;
  219. now = ktime_get_seconds();
  220. read_lock_bh(&client->ingress_lock);
  221. entry = client->in_cache;
  222. while (entry != NULL) {
  223. if (entry->entry_state == INGRESS_RESOLVING) {
  224. if ((now - entry->hold_down)
  225. < client->parameters.mpc_p6) {
  226. entry = entry->next; /* Entry in hold down */
  227. continue;
  228. }
  229. if ((now - entry->reply_wait) > entry->retry_time) {
  230. entry->retry_time = MPC_C1 * (entry->retry_time);
  231. /*
  232. * Retry time maximum exceeded,
  233. * put entry in hold down.
  234. */
  235. if (entry->retry_time > client->parameters.mpc_p5) {
  236. entry->hold_down = ktime_get_seconds();
  237. entry->retry_time = client->parameters.mpc_p4;
  238. entry = entry->next;
  239. continue;
  240. }
  241. /* Ask daemon to send a resolution request. */
  242. memset(&entry->hold_down, 0, sizeof(time64_t));
  243. msg.type = SND_MPOA_RES_RTRY;
  244. memcpy(msg.MPS_ctrl, client->mps_ctrl_addr, ATM_ESA_LEN);
  245. msg.content.in_info = entry->ctrl_info;
  246. qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
  247. if (qos != NULL)
  248. msg.qos = qos->qos;
  249. msg_to_mpoad(&msg, client);
  250. entry->reply_wait = ktime_get_seconds();
  251. }
  252. }
  253. entry = entry->next;
  254. }
  255. read_unlock_bh(&client->ingress_lock);
  256. }
  257. /* Call this every MPC-p5 seconds. */
  258. static void refresh_entries(struct mpoa_client *client)
  259. {
  260. time64_t now;
  261. struct in_cache_entry *entry = client->in_cache;
  262. ddprintk("refresh_entries\n");
  263. now = ktime_get_seconds();
  264. read_lock_bh(&client->ingress_lock);
  265. while (entry != NULL) {
  266. if (entry->entry_state == INGRESS_RESOLVED) {
  267. if (!(entry->refresh_time))
  268. entry->refresh_time = (2 * (entry->ctrl_info.holding_time))/3;
  269. if ((now - entry->reply_wait) >
  270. entry->refresh_time) {
  271. dprintk("refreshing an entry.\n");
  272. entry->entry_state = INGRESS_REFRESHING;
  273. }
  274. }
  275. entry = entry->next;
  276. }
  277. read_unlock_bh(&client->ingress_lock);
  278. }
  279. static void in_destroy_cache(struct mpoa_client *mpc)
  280. {
  281. write_lock_irq(&mpc->ingress_lock);
  282. while (mpc->in_cache != NULL)
  283. mpc->in_ops->remove_entry(mpc->in_cache, mpc);
  284. write_unlock_irq(&mpc->ingress_lock);
  285. }
  286. static eg_cache_entry *eg_cache_get_by_cache_id(__be32 cache_id,
  287. struct mpoa_client *mpc)
  288. {
  289. eg_cache_entry *entry;
  290. read_lock_irq(&mpc->egress_lock);
  291. entry = mpc->eg_cache;
  292. while (entry != NULL) {
  293. if (entry->ctrl_info.cache_id == cache_id) {
  294. refcount_inc(&entry->use);
  295. read_unlock_irq(&mpc->egress_lock);
  296. return entry;
  297. }
  298. entry = entry->next;
  299. }
  300. read_unlock_irq(&mpc->egress_lock);
  301. return NULL;
  302. }
  303. /* This can be called from any context since it saves CPU flags */
  304. static eg_cache_entry *eg_cache_get_by_tag(__be32 tag, struct mpoa_client *mpc)
  305. {
  306. unsigned long flags;
  307. eg_cache_entry *entry;
  308. read_lock_irqsave(&mpc->egress_lock, flags);
  309. entry = mpc->eg_cache;
  310. while (entry != NULL) {
  311. if (entry->ctrl_info.tag == tag) {
  312. refcount_inc(&entry->use);
  313. read_unlock_irqrestore(&mpc->egress_lock, flags);
  314. return entry;
  315. }
  316. entry = entry->next;
  317. }
  318. read_unlock_irqrestore(&mpc->egress_lock, flags);
  319. return NULL;
  320. }
  321. /* This can be called from any context since it saves CPU flags */
  322. static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc,
  323. struct mpoa_client *mpc)
  324. {
  325. unsigned long flags;
  326. eg_cache_entry *entry;
  327. read_lock_irqsave(&mpc->egress_lock, flags);
  328. entry = mpc->eg_cache;
  329. while (entry != NULL) {
  330. if (entry->shortcut == vcc) {
  331. refcount_inc(&entry->use);
  332. read_unlock_irqrestore(&mpc->egress_lock, flags);
  333. return entry;
  334. }
  335. entry = entry->next;
  336. }
  337. read_unlock_irqrestore(&mpc->egress_lock, flags);
  338. return NULL;
  339. }
  340. static eg_cache_entry *eg_cache_get_by_src_ip(__be32 ipaddr,
  341. struct mpoa_client *mpc)
  342. {
  343. eg_cache_entry *entry;
  344. read_lock_irq(&mpc->egress_lock);
  345. entry = mpc->eg_cache;
  346. while (entry != NULL) {
  347. if (entry->latest_ip_addr == ipaddr) {
  348. refcount_inc(&entry->use);
  349. read_unlock_irq(&mpc->egress_lock);
  350. return entry;
  351. }
  352. entry = entry->next;
  353. }
  354. read_unlock_irq(&mpc->egress_lock);
  355. return NULL;
  356. }
  357. static void eg_cache_put(eg_cache_entry *entry)
  358. {
  359. if (refcount_dec_and_test(&entry->use)) {
  360. kfree_sensitive(entry);
  361. }
  362. }
  363. /*
  364. * This should be called with write lock on
  365. */
  366. static void eg_cache_remove_entry(eg_cache_entry *entry,
  367. struct mpoa_client *client)
  368. {
  369. struct atm_vcc *vcc;
  370. struct k_message msg;
  371. vcc = entry->shortcut;
  372. dprintk("removing an egress entry.\n");
  373. if (entry->prev != NULL)
  374. entry->prev->next = entry->next;
  375. else
  376. client->eg_cache = entry->next;
  377. if (entry->next != NULL)
  378. entry->next->prev = entry->prev;
  379. client->eg_ops->put(entry);
  380. if (client->in_cache == NULL && client->eg_cache == NULL) {
  381. msg.type = STOP_KEEP_ALIVE_SM;
  382. msg_to_mpoad(&msg, client);
  383. }
  384. /* Check if the ingress side still uses this VCC */
  385. if (vcc != NULL) {
  386. in_cache_entry *in_entry = client->in_ops->get_by_vcc(vcc, client);
  387. if (in_entry != NULL) {
  388. client->in_ops->put(in_entry);
  389. return;
  390. }
  391. vcc_release_async(vcc, -EPIPE);
  392. }
  393. }
  394. static eg_cache_entry *eg_cache_add_entry(struct k_message *msg,
  395. struct mpoa_client *client)
  396. {
  397. eg_cache_entry *entry = kzalloc(sizeof(eg_cache_entry), GFP_KERNEL);
  398. if (entry == NULL) {
  399. pr_info("out of memory\n");
  400. return NULL;
  401. }
  402. dprintk("adding an egress entry, ip = %pI4, this should be our IP\n",
  403. &msg->content.eg_info.eg_dst_ip);
  404. refcount_set(&entry->use, 1);
  405. dprintk("new_eg_cache_entry: about to lock\n");
  406. write_lock_irq(&client->egress_lock);
  407. entry->next = client->eg_cache;
  408. entry->prev = NULL;
  409. if (client->eg_cache != NULL)
  410. client->eg_cache->prev = entry;
  411. client->eg_cache = entry;
  412. memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
  413. entry->ctrl_info = msg->content.eg_info;
  414. entry->time = ktime_get_seconds();
  415. entry->entry_state = EGRESS_RESOLVED;
  416. dprintk("new_eg_cache_entry cache_id %u\n",
  417. ntohl(entry->ctrl_info.cache_id));
  418. dprintk("mps_ip = %pI4\n", &entry->ctrl_info.mps_ip);
  419. refcount_inc(&entry->use);
  420. write_unlock_irq(&client->egress_lock);
  421. dprintk("new_eg_cache_entry: unlocked\n");
  422. return entry;
  423. }
  424. static void update_eg_cache_entry(eg_cache_entry *entry, uint16_t holding_time)
  425. {
  426. entry->time = ktime_get_seconds();
  427. entry->entry_state = EGRESS_RESOLVED;
  428. entry->ctrl_info.holding_time = holding_time;
  429. }
  430. static void clear_expired(struct mpoa_client *client)
  431. {
  432. eg_cache_entry *entry, *next_entry;
  433. time64_t now;
  434. struct k_message msg;
  435. now = ktime_get_seconds();
  436. write_lock_irq(&client->egress_lock);
  437. entry = client->eg_cache;
  438. while (entry != NULL) {
  439. next_entry = entry->next;
  440. if ((now - entry->time) > entry->ctrl_info.holding_time) {
  441. msg.type = SND_EGRESS_PURGE;
  442. msg.content.eg_info = entry->ctrl_info;
  443. dprintk("egress_cache: holding time expired, cache_id = %u.\n",
  444. ntohl(entry->ctrl_info.cache_id));
  445. msg_to_mpoad(&msg, client);
  446. client->eg_ops->remove_entry(entry, client);
  447. }
  448. entry = next_entry;
  449. }
  450. write_unlock_irq(&client->egress_lock);
  451. }
  452. static void eg_destroy_cache(struct mpoa_client *mpc)
  453. {
  454. write_lock_irq(&mpc->egress_lock);
  455. while (mpc->eg_cache != NULL)
  456. mpc->eg_ops->remove_entry(mpc->eg_cache, mpc);
  457. write_unlock_irq(&mpc->egress_lock);
  458. }
  459. static const struct in_cache_ops ingress_ops = {
  460. .add_entry = in_cache_add_entry,
  461. .get = in_cache_get,
  462. .get_with_mask = in_cache_get_with_mask,
  463. .get_by_vcc = in_cache_get_by_vcc,
  464. .put = in_cache_put,
  465. .remove_entry = in_cache_remove_entry,
  466. .cache_hit = cache_hit,
  467. .clear_count = clear_count_and_expired,
  468. .check_resolving = check_resolving_entries,
  469. .refresh = refresh_entries,
  470. .destroy_cache = in_destroy_cache
  471. };
  472. static const struct eg_cache_ops egress_ops = {
  473. .add_entry = eg_cache_add_entry,
  474. .get_by_cache_id = eg_cache_get_by_cache_id,
  475. .get_by_tag = eg_cache_get_by_tag,
  476. .get_by_vcc = eg_cache_get_by_vcc,
  477. .get_by_src_ip = eg_cache_get_by_src_ip,
  478. .put = eg_cache_put,
  479. .remove_entry = eg_cache_remove_entry,
  480. .update = update_eg_cache_entry,
  481. .clear_expired = clear_expired,
  482. .destroy_cache = eg_destroy_cache
  483. };
  484. void atm_mpoa_init_cache(struct mpoa_client *mpc)
  485. {
  486. mpc->in_ops = &ingress_ops;
  487. mpc->eg_ops = &egress_ops;
  488. }