mpoa_caches.c 15 KB

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