mpoa_caches.c 15 KB

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