mpoa_caches.c 15 KB

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