mpoa_caches.c 15 KB

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