mpoa_caches.c 14 KB

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