mpoa_caches.c 15 KB

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