sch_atm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /* net/sched/sch_atm.c - ATM VC selection "queueing discipline" */
  2. /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>
  6. #include <linux/errno.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/atmdev.h>
  9. #include <linux/atmclip.h>
  10. #include <linux/rtnetlink.h>
  11. #include <linux/file.h> /* for fput */
  12. #include <net/netlink.h>
  13. #include <net/pkt_sched.h>
  14. extern struct socket *sockfd_lookup(int fd, int *err); /* @@@ fix this */
  15. #if 0 /* control */
  16. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  17. #else
  18. #define DPRINTK(format,args...)
  19. #endif
  20. #if 0 /* data */
  21. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  22. #else
  23. #define D2PRINTK(format,args...)
  24. #endif
  25. /*
  26. * The ATM queuing discipline provides a framework for invoking classifiers
  27. * (aka "filters"), which in turn select classes of this queuing discipline.
  28. * Each class maps the flow(s) it is handling to a given VC. Multiple classes
  29. * may share the same VC.
  30. *
  31. * When creating a class, VCs are specified by passing the number of the open
  32. * socket descriptor by which the calling process references the VC. The kernel
  33. * keeps the VC open at least until all classes using it are removed.
  34. *
  35. * In this file, most functions are named atm_tc_* to avoid confusion with all
  36. * the atm_* in net/atm. This naming convention differs from what's used in the
  37. * rest of net/sched.
  38. *
  39. * Known bugs:
  40. * - sometimes messes up the IP stack
  41. * - any manipulations besides the few operations described in the README, are
  42. * untested and likely to crash the system
  43. * - should lock the flow while there is data in the queue (?)
  44. */
  45. #define PRIV(sch) qdisc_priv(sch)
  46. #define VCC2FLOW(vcc) ((struct atm_flow_data *) ((vcc)->user_back))
  47. struct atm_flow_data {
  48. struct Qdisc *q; /* FIFO, TBF, etc. */
  49. struct tcf_proto *filter_list;
  50. struct atm_vcc *vcc; /* VCC; NULL if VCC is closed */
  51. void (*old_pop)(struct atm_vcc *vcc,
  52. struct sk_buff * skb); /* chaining */
  53. struct atm_qdisc_data *parent; /* parent qdisc */
  54. struct socket *sock; /* for closing */
  55. u32 classid; /* x:y type ID */
  56. int ref; /* reference count */
  57. struct gnet_stats_basic bstats;
  58. struct gnet_stats_queue qstats;
  59. struct atm_flow_data *next;
  60. struct atm_flow_data *excess; /* flow for excess traffic;
  61. NULL to set CLP instead */
  62. int hdr_len;
  63. unsigned char hdr[0]; /* header data; MUST BE LAST */
  64. };
  65. struct atm_qdisc_data {
  66. struct atm_flow_data link; /* unclassified skbs go here */
  67. struct atm_flow_data *flows; /* NB: "link" is also on this
  68. list */
  69. struct tasklet_struct task; /* requeue tasklet */
  70. };
  71. /* ------------------------- Class/flow operations ------------------------- */
  72. static int find_flow(struct atm_qdisc_data *qdisc, struct atm_flow_data *flow)
  73. {
  74. struct atm_flow_data *walk;
  75. DPRINTK("find_flow(qdisc %p,flow %p)\n", qdisc, flow);
  76. for (walk = qdisc->flows; walk; walk = walk->next)
  77. if (walk == flow)
  78. return 1;
  79. DPRINTK("find_flow: not found\n");
  80. return 0;
  81. }
  82. static inline struct atm_flow_data *lookup_flow(struct Qdisc *sch, u32 classid)
  83. {
  84. struct atm_qdisc_data *p = PRIV(sch);
  85. struct atm_flow_data *flow;
  86. for (flow = p->flows; flow; flow = flow->next)
  87. if (flow->classid == classid)
  88. break;
  89. return flow;
  90. }
  91. static int atm_tc_graft(struct Qdisc *sch, unsigned long arg,
  92. struct Qdisc *new, struct Qdisc **old)
  93. {
  94. struct atm_qdisc_data *p = PRIV(sch);
  95. struct atm_flow_data *flow = (struct atm_flow_data *)arg;
  96. DPRINTK("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",
  97. sch, p, flow, new, old);
  98. if (!find_flow(p, flow))
  99. return -EINVAL;
  100. if (!new)
  101. new = &noop_qdisc;
  102. *old = xchg(&flow->q, new);
  103. if (*old)
  104. qdisc_reset(*old);
  105. return 0;
  106. }
  107. static struct Qdisc *atm_tc_leaf(struct Qdisc *sch, unsigned long cl)
  108. {
  109. struct atm_flow_data *flow = (struct atm_flow_data *)cl;
  110. DPRINTK("atm_tc_leaf(sch %p,flow %p)\n", sch, flow);
  111. return flow ? flow->q : NULL;
  112. }
  113. static unsigned long atm_tc_get(struct Qdisc *sch, u32 classid)
  114. {
  115. struct atm_qdisc_data *p __maybe_unused = PRIV(sch);
  116. struct atm_flow_data *flow;
  117. DPRINTK("atm_tc_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
  118. flow = lookup_flow(sch, classid);
  119. if (flow)
  120. flow->ref++;
  121. DPRINTK("atm_tc_get: flow %p\n", flow);
  122. return (unsigned long)flow;
  123. }
  124. static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
  125. unsigned long parent, u32 classid)
  126. {
  127. return atm_tc_get(sch, classid);
  128. }
  129. /*
  130. * atm_tc_put handles all destructions, including the ones that are explicitly
  131. * requested (atm_tc_destroy, etc.). The assumption here is that we never drop
  132. * anything that still seems to be in use.
  133. */
  134. static void atm_tc_put(struct Qdisc *sch, unsigned long cl)
  135. {
  136. struct atm_qdisc_data *p = PRIV(sch);
  137. struct atm_flow_data *flow = (struct atm_flow_data *)cl;
  138. struct atm_flow_data **prev;
  139. DPRINTK("atm_tc_put(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
  140. if (--flow->ref)
  141. return;
  142. DPRINTK("atm_tc_put: destroying\n");
  143. for (prev = &p->flows; *prev; prev = &(*prev)->next)
  144. if (*prev == flow)
  145. break;
  146. if (!*prev) {
  147. printk(KERN_CRIT "atm_tc_put: class %p not found\n", flow);
  148. return;
  149. }
  150. *prev = flow->next;
  151. DPRINTK("atm_tc_put: qdisc %p\n", flow->q);
  152. qdisc_destroy(flow->q);
  153. tcf_destroy_chain(flow->filter_list);
  154. if (flow->sock) {
  155. DPRINTK("atm_tc_put: f_count %d\n",
  156. file_count(flow->sock->file));
  157. flow->vcc->pop = flow->old_pop;
  158. sockfd_put(flow->sock);
  159. }
  160. if (flow->excess)
  161. atm_tc_put(sch, (unsigned long)flow->excess);
  162. if (flow != &p->link)
  163. kfree(flow);
  164. /*
  165. * If flow == &p->link, the qdisc no longer works at this point and
  166. * needs to be removed. (By the caller of atm_tc_put.)
  167. */
  168. }
  169. static void sch_atm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  170. {
  171. struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;
  172. D2PRINTK("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n", vcc, skb, p);
  173. VCC2FLOW(vcc)->old_pop(vcc, skb);
  174. tasklet_schedule(&p->task);
  175. }
  176. static const u8 llc_oui_ip[] = {
  177. 0xaa, /* DSAP: non-ISO */
  178. 0xaa, /* SSAP: non-ISO */
  179. 0x03, /* Ctrl: Unnumbered Information Command PDU */
  180. 0x00, /* OUI: EtherType */
  181. 0x00, 0x00,
  182. 0x08, 0x00
  183. }; /* Ethertype IP (0800) */
  184. static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent,
  185. struct rtattr **tca, unsigned long *arg)
  186. {
  187. struct atm_qdisc_data *p = PRIV(sch);
  188. struct atm_flow_data *flow = (struct atm_flow_data *)*arg;
  189. struct atm_flow_data *excess = NULL;
  190. struct rtattr *opt = tca[TCA_OPTIONS - 1];
  191. struct rtattr *tb[TCA_ATM_MAX];
  192. struct socket *sock;
  193. int fd, error, hdr_len;
  194. void *hdr;
  195. DPRINTK("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
  196. "flow %p,opt %p)\n", sch, p, classid, parent, flow, opt);
  197. /*
  198. * The concept of parents doesn't apply for this qdisc.
  199. */
  200. if (parent && parent != TC_H_ROOT && parent != sch->handle)
  201. return -EINVAL;
  202. /*
  203. * ATM classes cannot be changed. In order to change properties of the
  204. * ATM connection, that socket needs to be modified directly (via the
  205. * native ATM API. In order to send a flow to a different VC, the old
  206. * class needs to be removed and a new one added. (This may be changed
  207. * later.)
  208. */
  209. if (flow)
  210. return -EBUSY;
  211. if (opt == NULL || rtattr_parse_nested(tb, TCA_ATM_MAX, opt))
  212. return -EINVAL;
  213. if (!tb[TCA_ATM_FD - 1] || RTA_PAYLOAD(tb[TCA_ATM_FD - 1]) < sizeof(fd))
  214. return -EINVAL;
  215. fd = *(int *)RTA_DATA(tb[TCA_ATM_FD - 1]);
  216. DPRINTK("atm_tc_change: fd %d\n", fd);
  217. if (tb[TCA_ATM_HDR - 1]) {
  218. hdr_len = RTA_PAYLOAD(tb[TCA_ATM_HDR - 1]);
  219. hdr = RTA_DATA(tb[TCA_ATM_HDR - 1]);
  220. } else {
  221. hdr_len = RFC1483LLC_LEN;
  222. hdr = NULL; /* default LLC/SNAP for IP */
  223. }
  224. if (!tb[TCA_ATM_EXCESS - 1])
  225. excess = NULL;
  226. else {
  227. if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS - 1]) != sizeof(u32))
  228. return -EINVAL;
  229. excess = (struct atm_flow_data *)
  230. atm_tc_get(sch, *(u32 *)RTA_DATA(tb[TCA_ATM_EXCESS - 1]));
  231. if (!excess)
  232. return -ENOENT;
  233. }
  234. DPRINTK("atm_tc_change: type %d, payload %d, hdr_len %d\n",
  235. opt->rta_type, RTA_PAYLOAD(opt), hdr_len);
  236. if (!(sock = sockfd_lookup(fd, &error)))
  237. return error; /* f_count++ */
  238. DPRINTK("atm_tc_change: f_count %d\n", file_count(sock->file));
  239. if (sock->ops->family != PF_ATMSVC && sock->ops->family != PF_ATMPVC) {
  240. error = -EPROTOTYPE;
  241. goto err_out;
  242. }
  243. /* @@@ should check if the socket is really operational or we'll crash
  244. on vcc->send */
  245. if (classid) {
  246. if (TC_H_MAJ(classid ^ sch->handle)) {
  247. DPRINTK("atm_tc_change: classid mismatch\n");
  248. error = -EINVAL;
  249. goto err_out;
  250. }
  251. if (find_flow(p, flow)) {
  252. error = -EEXIST;
  253. goto err_out;
  254. }
  255. } else {
  256. int i;
  257. unsigned long cl;
  258. for (i = 1; i < 0x8000; i++) {
  259. classid = TC_H_MAKE(sch->handle, 0x8000 | i);
  260. if (!(cl = atm_tc_get(sch, classid)))
  261. break;
  262. atm_tc_put(sch, cl);
  263. }
  264. }
  265. DPRINTK("atm_tc_change: new id %x\n", classid);
  266. flow = kzalloc(sizeof(struct atm_flow_data) + hdr_len, GFP_KERNEL);
  267. DPRINTK("atm_tc_change: flow %p\n", flow);
  268. if (!flow) {
  269. error = -ENOBUFS;
  270. goto err_out;
  271. }
  272. flow->filter_list = NULL;
  273. if (!(flow->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid)))
  274. flow->q = &noop_qdisc;
  275. DPRINTK("atm_tc_change: qdisc %p\n", flow->q);
  276. flow->sock = sock;
  277. flow->vcc = ATM_SD(sock); /* speedup */
  278. flow->vcc->user_back = flow;
  279. DPRINTK("atm_tc_change: vcc %p\n", flow->vcc);
  280. flow->old_pop = flow->vcc->pop;
  281. flow->parent = p;
  282. flow->vcc->pop = sch_atm_pop;
  283. flow->classid = classid;
  284. flow->ref = 1;
  285. flow->excess = excess;
  286. flow->next = p->link.next;
  287. p->link.next = flow;
  288. flow->hdr_len = hdr_len;
  289. if (hdr)
  290. memcpy(flow->hdr, hdr, hdr_len);
  291. else
  292. memcpy(flow->hdr, llc_oui_ip, sizeof(llc_oui_ip));
  293. *arg = (unsigned long)flow;
  294. return 0;
  295. err_out:
  296. if (excess)
  297. atm_tc_put(sch, (unsigned long)excess);
  298. sockfd_put(sock);
  299. return error;
  300. }
  301. static int atm_tc_delete(struct Qdisc *sch, unsigned long arg)
  302. {
  303. struct atm_qdisc_data *p = PRIV(sch);
  304. struct atm_flow_data *flow = (struct atm_flow_data *)arg;
  305. DPRINTK("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
  306. if (!find_flow(PRIV(sch), flow))
  307. return -EINVAL;
  308. if (flow->filter_list || flow == &p->link)
  309. return -EBUSY;
  310. /*
  311. * Reference count must be 2: one for "keepalive" (set at class
  312. * creation), and one for the reference held when calling delete.
  313. */
  314. if (flow->ref < 2) {
  315. printk(KERN_ERR "atm_tc_delete: flow->ref == %d\n", flow->ref);
  316. return -EINVAL;
  317. }
  318. if (flow->ref > 2)
  319. return -EBUSY; /* catch references via excess, etc. */
  320. atm_tc_put(sch, arg);
  321. return 0;
  322. }
  323. static void atm_tc_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  324. {
  325. struct atm_qdisc_data *p = PRIV(sch);
  326. struct atm_flow_data *flow;
  327. DPRINTK("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
  328. if (walker->stop)
  329. return;
  330. for (flow = p->flows; flow; flow = flow->next) {
  331. if (walker->count >= walker->skip)
  332. if (walker->fn(sch, (unsigned long)flow, walker) < 0) {
  333. walker->stop = 1;
  334. break;
  335. }
  336. walker->count++;
  337. }
  338. }
  339. static struct tcf_proto **atm_tc_find_tcf(struct Qdisc *sch, unsigned long cl)
  340. {
  341. struct atm_qdisc_data *p = PRIV(sch);
  342. struct atm_flow_data *flow = (struct atm_flow_data *)cl;
  343. DPRINTK("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
  344. return flow ? &flow->filter_list : &p->link.filter_list;
  345. }
  346. /* --------------------------- Qdisc operations ---------------------------- */
  347. static int atm_tc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  348. {
  349. struct atm_qdisc_data *p = PRIV(sch);
  350. struct atm_flow_data *flow = NULL; /* @@@ */
  351. struct tcf_result res;
  352. int result;
  353. int ret = NET_XMIT_POLICED;
  354. D2PRINTK("atm_tc_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  355. result = TC_POLICE_OK; /* be nice to gcc */
  356. if (TC_H_MAJ(skb->priority) != sch->handle ||
  357. !(flow = (struct atm_flow_data *)atm_tc_get(sch, skb->priority)))
  358. for (flow = p->flows; flow; flow = flow->next)
  359. if (flow->filter_list) {
  360. result = tc_classify_compat(skb,
  361. flow->filter_list,
  362. &res);
  363. if (result < 0)
  364. continue;
  365. flow = (struct atm_flow_data *)res.class;
  366. if (!flow)
  367. flow = lookup_flow(sch, res.classid);
  368. break;
  369. }
  370. if (!flow)
  371. flow = &p->link;
  372. else {
  373. if (flow->vcc)
  374. ATM_SKB(skb)->atm_options = flow->vcc->atm_options;
  375. /*@@@ looks good ... but it's not supposed to work :-) */
  376. #ifdef CONFIG_NET_CLS_ACT
  377. switch (result) {
  378. case TC_ACT_QUEUED:
  379. case TC_ACT_STOLEN:
  380. kfree_skb(skb);
  381. return NET_XMIT_SUCCESS;
  382. case TC_ACT_SHOT:
  383. kfree_skb(skb);
  384. goto drop;
  385. case TC_POLICE_RECLASSIFY:
  386. if (flow->excess)
  387. flow = flow->excess;
  388. else
  389. ATM_SKB(skb)->atm_options |= ATM_ATMOPT_CLP;
  390. break;
  391. }
  392. #endif
  393. }
  394. if ((ret = flow->q->enqueue(skb, flow->q)) != 0) {
  395. drop: __maybe_unused
  396. sch->qstats.drops++;
  397. if (flow)
  398. flow->qstats.drops++;
  399. return ret;
  400. }
  401. sch->bstats.bytes += skb->len;
  402. sch->bstats.packets++;
  403. flow->bstats.bytes += skb->len;
  404. flow->bstats.packets++;
  405. /*
  406. * Okay, this may seem weird. We pretend we've dropped the packet if
  407. * it goes via ATM. The reason for this is that the outer qdisc
  408. * expects to be able to q->dequeue the packet later on if we return
  409. * success at this place. Also, sch->q.qdisc needs to reflect whether
  410. * there is a packet egligible for dequeuing or not. Note that the
  411. * statistics of the outer qdisc are necessarily wrong because of all
  412. * this. There's currently no correct solution for this.
  413. */
  414. if (flow == &p->link) {
  415. sch->q.qlen++;
  416. return 0;
  417. }
  418. tasklet_schedule(&p->task);
  419. return NET_XMIT_BYPASS;
  420. }
  421. /*
  422. * Dequeue packets and send them over ATM. Note that we quite deliberately
  423. * avoid checking net_device's flow control here, simply because sch_atm
  424. * uses its own channels, which have nothing to do with any CLIP/LANE/or
  425. * non-ATM interfaces.
  426. */
  427. static void sch_atm_dequeue(unsigned long data)
  428. {
  429. struct Qdisc *sch = (struct Qdisc *)data;
  430. struct atm_qdisc_data *p = PRIV(sch);
  431. struct atm_flow_data *flow;
  432. struct sk_buff *skb;
  433. D2PRINTK("sch_atm_dequeue(sch %p,[qdisc %p])\n", sch, p);
  434. for (flow = p->link.next; flow; flow = flow->next)
  435. /*
  436. * If traffic is properly shaped, this won't generate nasty
  437. * little bursts. Otherwise, it may ... (but that's okay)
  438. */
  439. while ((skb = flow->q->dequeue(flow->q))) {
  440. if (!atm_may_send(flow->vcc, skb->truesize)) {
  441. (void)flow->q->ops->requeue(skb, flow->q);
  442. break;
  443. }
  444. D2PRINTK("atm_tc_dequeue: sending on class %p\n", flow);
  445. /* remove any LL header somebody else has attached */
  446. skb_pull(skb, skb_network_offset(skb));
  447. if (skb_headroom(skb) < flow->hdr_len) {
  448. struct sk_buff *new;
  449. new = skb_realloc_headroom(skb, flow->hdr_len);
  450. dev_kfree_skb(skb);
  451. if (!new)
  452. continue;
  453. skb = new;
  454. }
  455. D2PRINTK("sch_atm_dequeue: ip %p, data %p\n",
  456. skb_network_header(skb), skb->data);
  457. ATM_SKB(skb)->vcc = flow->vcc;
  458. memcpy(skb_push(skb, flow->hdr_len), flow->hdr,
  459. flow->hdr_len);
  460. atomic_add(skb->truesize,
  461. &sk_atm(flow->vcc)->sk_wmem_alloc);
  462. /* atm.atm_options are already set by atm_tc_enqueue */
  463. flow->vcc->send(flow->vcc, skb);
  464. }
  465. }
  466. static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
  467. {
  468. struct atm_qdisc_data *p = PRIV(sch);
  469. struct sk_buff *skb;
  470. D2PRINTK("atm_tc_dequeue(sch %p,[qdisc %p])\n", sch, p);
  471. tasklet_schedule(&p->task);
  472. skb = p->link.q->dequeue(p->link.q);
  473. if (skb)
  474. sch->q.qlen--;
  475. return skb;
  476. }
  477. static int atm_tc_requeue(struct sk_buff *skb, struct Qdisc *sch)
  478. {
  479. struct atm_qdisc_data *p = PRIV(sch);
  480. int ret;
  481. D2PRINTK("atm_tc_requeue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  482. ret = p->link.q->ops->requeue(skb, p->link.q);
  483. if (!ret) {
  484. sch->q.qlen++;
  485. sch->qstats.requeues++;
  486. } else {
  487. sch->qstats.drops++;
  488. p->link.qstats.drops++;
  489. }
  490. return ret;
  491. }
  492. static unsigned int atm_tc_drop(struct Qdisc *sch)
  493. {
  494. struct atm_qdisc_data *p = PRIV(sch);
  495. struct atm_flow_data *flow;
  496. unsigned int len;
  497. DPRINTK("atm_tc_drop(sch %p,[qdisc %p])\n", sch, p);
  498. for (flow = p->flows; flow; flow = flow->next)
  499. if (flow->q->ops->drop && (len = flow->q->ops->drop(flow->q)))
  500. return len;
  501. return 0;
  502. }
  503. static int atm_tc_init(struct Qdisc *sch, struct rtattr *opt)
  504. {
  505. struct atm_qdisc_data *p = PRIV(sch);
  506. DPRINTK("atm_tc_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
  507. p->flows = &p->link;
  508. if (!(p->link.q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  509. sch->handle)))
  510. p->link.q = &noop_qdisc;
  511. DPRINTK("atm_tc_init: link (%p) qdisc %p\n", &p->link, p->link.q);
  512. p->link.filter_list = NULL;
  513. p->link.vcc = NULL;
  514. p->link.sock = NULL;
  515. p->link.classid = sch->handle;
  516. p->link.ref = 1;
  517. p->link.next = NULL;
  518. tasklet_init(&p->task, sch_atm_dequeue, (unsigned long)sch);
  519. return 0;
  520. }
  521. static void atm_tc_reset(struct Qdisc *sch)
  522. {
  523. struct atm_qdisc_data *p = PRIV(sch);
  524. struct atm_flow_data *flow;
  525. DPRINTK("atm_tc_reset(sch %p,[qdisc %p])\n", sch, p);
  526. for (flow = p->flows; flow; flow = flow->next)
  527. qdisc_reset(flow->q);
  528. sch->q.qlen = 0;
  529. }
  530. static void atm_tc_destroy(struct Qdisc *sch)
  531. {
  532. struct atm_qdisc_data *p = PRIV(sch);
  533. struct atm_flow_data *flow;
  534. DPRINTK("atm_tc_destroy(sch %p,[qdisc %p])\n", sch, p);
  535. /* races ? */
  536. while ((flow = p->flows)) {
  537. tcf_destroy_chain(flow->filter_list);
  538. flow->filter_list = NULL;
  539. if (flow->ref > 1)
  540. printk(KERN_ERR "atm_destroy: %p->ref = %d\n", flow,
  541. flow->ref);
  542. atm_tc_put(sch, (unsigned long)flow);
  543. if (p->flows == flow) {
  544. printk(KERN_ERR "atm_destroy: putting flow %p didn't "
  545. "kill it\n", flow);
  546. p->flows = flow->next; /* brute force */
  547. break;
  548. }
  549. }
  550. tasklet_kill(&p->task);
  551. }
  552. static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
  553. struct sk_buff *skb, struct tcmsg *tcm)
  554. {
  555. struct atm_qdisc_data *p = PRIV(sch);
  556. struct atm_flow_data *flow = (struct atm_flow_data *)cl;
  557. unsigned char *b = skb_tail_pointer(skb);
  558. struct rtattr *rta;
  559. DPRINTK("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
  560. sch, p, flow, skb, tcm);
  561. if (!find_flow(p, flow))
  562. return -EINVAL;
  563. tcm->tcm_handle = flow->classid;
  564. tcm->tcm_info = flow->q->handle;
  565. rta = (struct rtattr *)b;
  566. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  567. RTA_PUT(skb, TCA_ATM_HDR, flow->hdr_len, flow->hdr);
  568. if (flow->vcc) {
  569. struct sockaddr_atmpvc pvc;
  570. int state;
  571. pvc.sap_family = AF_ATMPVC;
  572. pvc.sap_addr.itf = flow->vcc->dev ? flow->vcc->dev->number : -1;
  573. pvc.sap_addr.vpi = flow->vcc->vpi;
  574. pvc.sap_addr.vci = flow->vcc->vci;
  575. RTA_PUT(skb, TCA_ATM_ADDR, sizeof(pvc), &pvc);
  576. state = ATM_VF2VS(flow->vcc->flags);
  577. RTA_PUT(skb, TCA_ATM_STATE, sizeof(state), &state);
  578. }
  579. if (flow->excess)
  580. RTA_PUT(skb, TCA_ATM_EXCESS, sizeof(u32), &flow->classid);
  581. else {
  582. static u32 zero;
  583. RTA_PUT(skb, TCA_ATM_EXCESS, sizeof(zero), &zero);
  584. }
  585. rta->rta_len = skb_tail_pointer(skb) - b;
  586. return skb->len;
  587. rtattr_failure:
  588. nlmsg_trim(skb, b);
  589. return -1;
  590. }
  591. static int
  592. atm_tc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  593. struct gnet_dump *d)
  594. {
  595. struct atm_flow_data *flow = (struct atm_flow_data *)arg;
  596. flow->qstats.qlen = flow->q->q.qlen;
  597. if (gnet_stats_copy_basic(d, &flow->bstats) < 0 ||
  598. gnet_stats_copy_queue(d, &flow->qstats) < 0)
  599. return -1;
  600. return 0;
  601. }
  602. static int atm_tc_dump(struct Qdisc *sch, struct sk_buff *skb)
  603. {
  604. return 0;
  605. }
  606. static struct Qdisc_class_ops atm_class_ops = {
  607. .graft = atm_tc_graft,
  608. .leaf = atm_tc_leaf,
  609. .get = atm_tc_get,
  610. .put = atm_tc_put,
  611. .change = atm_tc_change,
  612. .delete = atm_tc_delete,
  613. .walk = atm_tc_walk,
  614. .tcf_chain = atm_tc_find_tcf,
  615. .bind_tcf = atm_tc_bind_filter,
  616. .unbind_tcf = atm_tc_put,
  617. .dump = atm_tc_dump_class,
  618. .dump_stats = atm_tc_dump_class_stats,
  619. };
  620. static struct Qdisc_ops atm_qdisc_ops = {
  621. .cl_ops = &atm_class_ops,
  622. .id = "atm",
  623. .priv_size = sizeof(struct atm_qdisc_data),
  624. .enqueue = atm_tc_enqueue,
  625. .dequeue = atm_tc_dequeue,
  626. .requeue = atm_tc_requeue,
  627. .drop = atm_tc_drop,
  628. .init = atm_tc_init,
  629. .reset = atm_tc_reset,
  630. .destroy = atm_tc_destroy,
  631. .dump = atm_tc_dump,
  632. .owner = THIS_MODULE,
  633. };
  634. static int __init atm_init(void)
  635. {
  636. return register_qdisc(&atm_qdisc_ops);
  637. }
  638. static void __exit atm_exit(void)
  639. {
  640. unregister_qdisc(&atm_qdisc_ops);
  641. }
  642. module_init(atm_init)
  643. module_exit(atm_exit)
  644. MODULE_LICENSE("GPL");