mpoa_caches.c 15 KB

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