mpoa_caches.c 15 KB

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