1
0

swconfig.patch 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. diff -Nur linux-2.6.30.orig/drivers/net/phy/Kconfig linux-2.6.30/drivers/net/phy/Kconfig
  2. --- linux-2.6.30.orig/drivers/net/phy/Kconfig 2009-06-10 05:05:27.000000000 +0200
  3. +++ linux-2.6.30/drivers/net/phy/Kconfig 2009-06-11 09:22:50.000000000 +0200
  4. @@ -13,6 +13,12 @@
  5. if PHYLIB
  6. +config SWCONFIG
  7. + tristate "Switch configuration API"
  8. + ---help---
  9. + Switch configuration API using netlink. This allows
  10. + you to configure the VLAN features of certain switches.
  11. +
  12. comment "MII PHY device drivers"
  13. config MARVELL_PHY
  14. diff -Nur linux-2.6.30.orig/drivers/net/phy/Makefile linux-2.6.30/drivers/net/phy/Makefile
  15. --- linux-2.6.30.orig/drivers/net/phy/Makefile 2009-06-10 05:05:27.000000000 +0200
  16. +++ linux-2.6.30/drivers/net/phy/Makefile 2009-06-11 09:22:50.000000000 +0200
  17. @@ -3,6 +3,7 @@
  18. libphy-objs := phy.o phy_device.o mdio_bus.o
  19. obj-$(CONFIG_PHYLIB) += libphy.o
  20. +obj-$(CONFIG_SWCONFIG) += swconfig.o
  21. obj-$(CONFIG_MARVELL_PHY) += marvell.o
  22. obj-$(CONFIG_DAVICOM_PHY) += davicom.o
  23. obj-$(CONFIG_CICADA_PHY) += cicada.o
  24. diff -Nur linux-2.6.30.orig/drivers/net/phy/swconfig.c linux-2.6.30/drivers/net/phy/swconfig.c
  25. --- linux-2.6.30.orig/drivers/net/phy/swconfig.c 1970-01-01 01:00:00.000000000 +0100
  26. +++ linux-2.6.30/drivers/net/phy/swconfig.c 2009-06-11 09:22:50.000000000 +0200
  27. @@ -0,0 +1,872 @@
  28. +/*
  29. + * swconfig.c: Switch configuration API
  30. + *
  31. + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  32. + *
  33. + * This program is free software; you can redistribute it and/or
  34. + * modify it under the terms of the GNU General Public License
  35. + * as published by the Free Software Foundation; either version 2
  36. + * of the License, or (at your option) any later version.
  37. + *
  38. + * This program is distributed in the hope that it will be useful,
  39. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. + * GNU General Public License for more details.
  42. + */
  43. +
  44. +#include <linux/types.h>
  45. +#include <linux/module.h>
  46. +#include <linux/init.h>
  47. +#include <linux/list.h>
  48. +#include <linux/if.h>
  49. +#include <linux/if_ether.h>
  50. +#include <linux/capability.h>
  51. +#include <linux/skbuff.h>
  52. +#include <linux/switch.h>
  53. +
  54. +//#define DEBUG 1
  55. +#ifdef DEBUG
  56. +#define DPRINTF(format, ...) printk("%s: " format, __func__, ##__VA_ARGS__)
  57. +#else
  58. +#define DPRINTF(...) do {} while(0)
  59. +#endif
  60. +
  61. +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  62. +MODULE_LICENSE("GPL");
  63. +
  64. +static int swdev_id = 0;
  65. +static struct list_head swdevs;
  66. +static spinlock_t swdevs_lock = SPIN_LOCK_UNLOCKED;
  67. +struct swconfig_callback;
  68. +
  69. +struct swconfig_callback
  70. +{
  71. + struct sk_buff *msg;
  72. + struct genlmsghdr *hdr;
  73. + struct genl_info *info;
  74. + int cmd;
  75. +
  76. + /* callback for filling in the message data */
  77. + int (*fill)(struct swconfig_callback *cb, void *arg);
  78. +
  79. + /* callback for closing the message before sending it */
  80. + int (*close)(struct swconfig_callback *cb, void *arg);
  81. +
  82. + struct nlattr *nest[4];
  83. + int args[4];
  84. +};
  85. +
  86. +/* defaults */
  87. +
  88. +static int
  89. +swconfig_get_vlan_ports(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  90. +{
  91. + int ret;
  92. + if (val->port_vlan >= dev->vlans)
  93. + return -EINVAL;
  94. +
  95. + if (!dev->get_vlan_ports)
  96. + return -EOPNOTSUPP;
  97. +
  98. + ret = dev->get_vlan_ports(dev, val);
  99. + printk("SET PORTS %d\n", val->len);
  100. + return ret;
  101. +}
  102. +
  103. +static int
  104. +swconfig_set_vlan_ports(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  105. +{
  106. + int i;
  107. +
  108. + if (val->port_vlan >= dev->vlans)
  109. + return -EINVAL;
  110. +
  111. + /* validate ports */
  112. + if (val->len > dev->ports)
  113. + return -EINVAL;
  114. +
  115. + for (i = 0; i < val->len; i++) {
  116. + if (val->value.ports[i].id >= dev->ports)
  117. + return -EINVAL;
  118. + }
  119. +
  120. + if (!dev->set_vlan_ports)
  121. + return -EOPNOTSUPP;
  122. +
  123. + printk("SET PORTS %d\n", val->len);
  124. + return dev->set_vlan_ports(dev, val);
  125. +}
  126. +
  127. +static int
  128. +swconfig_apply_config(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  129. +{
  130. + /* don't complain if not supported by the switch driver */
  131. + if (!dev->apply_config)
  132. + return 0;
  133. +
  134. + return dev->apply_config(dev);
  135. +}
  136. +
  137. +
  138. +enum global_defaults {
  139. + GLOBAL_APPLY,
  140. +};
  141. +
  142. +enum vlan_defaults {
  143. + VLAN_PORTS,
  144. +};
  145. +
  146. +enum port_defaults {
  147. + PORT_LINK,
  148. +};
  149. +
  150. +static struct switch_attr default_global[] = {
  151. + [GLOBAL_APPLY] = {
  152. + .type = SWITCH_TYPE_NOVAL,
  153. + .name = "apply",
  154. + .description = "Activate changes in the hardware",
  155. + .set = swconfig_apply_config,
  156. + }
  157. +};
  158. +
  159. +static struct switch_attr default_port[] = {
  160. + [PORT_LINK] = {
  161. + .type = SWITCH_TYPE_INT,
  162. + .name = "link",
  163. + .description = "Current link speed",
  164. + }
  165. +};
  166. +
  167. +static struct switch_attr default_vlan[] = {
  168. + [VLAN_PORTS] = {
  169. + .type = SWITCH_TYPE_PORTS,
  170. + .name = "ports",
  171. + .description = "VLAN port mapping",
  172. + .set = swconfig_set_vlan_ports,
  173. + .get = swconfig_get_vlan_ports,
  174. + },
  175. +};
  176. +
  177. +
  178. +static void swconfig_defaults_init(struct switch_dev *dev)
  179. +{
  180. + dev->def_global = 0;
  181. + dev->def_vlan = 0;
  182. + dev->def_port = 0;
  183. +
  184. + if (dev->get_vlan_ports || dev->set_vlan_ports)
  185. + set_bit(VLAN_PORTS, &dev->def_vlan);
  186. +
  187. + /* always present, can be no-op */
  188. + set_bit(GLOBAL_APPLY, &dev->def_global);
  189. +}
  190. +
  191. +
  192. +static struct genl_family switch_fam = {
  193. + .id = GENL_ID_GENERATE,
  194. + .name = "switch",
  195. + .hdrsize = 0,
  196. + .version = 1,
  197. + .maxattr = SWITCH_ATTR_MAX,
  198. +};
  199. +
  200. +static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
  201. + [SWITCH_ATTR_ID] = { .type = NLA_U32 },
  202. + [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
  203. + [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
  204. + [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
  205. + [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
  206. + [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
  207. + [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
  208. + [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
  209. +};
  210. +
  211. +static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
  212. + [SWITCH_PORT_ID] = { .type = NLA_U32 },
  213. + [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
  214. +};
  215. +
  216. +static inline void
  217. +swconfig_lock(void)
  218. +{
  219. + spin_lock(&swdevs_lock);
  220. +}
  221. +
  222. +static inline void
  223. +swconfig_unlock(void)
  224. +{
  225. + spin_unlock(&swdevs_lock);
  226. +}
  227. +
  228. +static struct switch_dev *
  229. +swconfig_get_dev(struct genl_info *info)
  230. +{
  231. + struct switch_dev *dev = NULL;
  232. + struct switch_dev *p;
  233. + int id;
  234. +
  235. + if (!info->attrs[SWITCH_ATTR_ID])
  236. + goto done;
  237. +
  238. + id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
  239. + swconfig_lock();
  240. + list_for_each_entry(p, &swdevs, dev_list) {
  241. + if (id != p->id)
  242. + continue;
  243. +
  244. + dev = p;
  245. + break;
  246. + }
  247. + if (dev)
  248. + spin_lock(&dev->lock);
  249. + else
  250. + DPRINTF("device %d not found\n", id);
  251. + swconfig_unlock();
  252. +done:
  253. + return dev;
  254. +}
  255. +
  256. +static inline void
  257. +swconfig_put_dev(struct switch_dev *dev)
  258. +{
  259. + spin_unlock(&dev->lock);
  260. +}
  261. +
  262. +static int
  263. +swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
  264. +{
  265. + struct switch_attr *op = arg;
  266. + struct genl_info *info = cb->info;
  267. + struct sk_buff *msg = cb->msg;
  268. + int id = cb->args[0];
  269. + void *hdr;
  270. +
  271. + hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
  272. + NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
  273. + if (IS_ERR(hdr))
  274. + return -1;
  275. +
  276. + NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
  277. + NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
  278. + NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
  279. + if (op->description)
  280. + NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
  281. + op->description);
  282. +
  283. + return genlmsg_end(msg, hdr);
  284. +nla_put_failure:
  285. + genlmsg_cancel(msg, hdr);
  286. + return -EMSGSIZE;
  287. +}
  288. +
  289. +/* spread multipart messages across multiple message buffers */
  290. +static int
  291. +swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
  292. +{
  293. + struct genl_info *info = cb->info;
  294. + int restart = 0;
  295. + int err;
  296. +
  297. + do {
  298. + if (!cb->msg) {
  299. + cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  300. + if (cb->msg == NULL)
  301. + goto error;
  302. + }
  303. +
  304. + if (!(cb->fill(cb, arg) < 0))
  305. + break;
  306. +
  307. + /* fill failed, check if this was already the second attempt */
  308. + if (restart)
  309. + goto error;
  310. +
  311. + /* try again in a new message, send the current one */
  312. + restart = 1;
  313. + if (cb->close) {
  314. + if (cb->close(cb, arg) < 0)
  315. + goto error;
  316. + }
  317. + err = genlmsg_unicast(cb->msg, info->snd_pid);
  318. + cb->msg = NULL;
  319. + if (err < 0)
  320. + goto error;
  321. +
  322. + } while (restart);
  323. +
  324. + return 0;
  325. +
  326. +error:
  327. + if (cb->msg)
  328. + nlmsg_free(cb->msg);
  329. + return -1;
  330. +}
  331. +
  332. +static int
  333. +swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
  334. +{
  335. + struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  336. + const struct switch_attrlist *alist;
  337. + struct switch_dev *dev;
  338. + struct swconfig_callback cb;
  339. + int err = -EINVAL;
  340. + int i;
  341. +
  342. + /* defaults */
  343. + struct switch_attr *def_list;
  344. + unsigned long *def_active;
  345. + int n_def;
  346. +
  347. + dev = swconfig_get_dev(info);
  348. + if (!dev)
  349. + return -EINVAL;
  350. +
  351. + switch(hdr->cmd) {
  352. + case SWITCH_CMD_LIST_GLOBAL:
  353. + alist = &dev->attr_global;
  354. + def_list = default_global;
  355. + def_active = &dev->def_global;
  356. + n_def = ARRAY_SIZE(default_global);
  357. + break;
  358. + case SWITCH_CMD_LIST_VLAN:
  359. + alist = &dev->attr_vlan;
  360. + def_list = default_vlan;
  361. + def_active = &dev->def_vlan;
  362. + n_def = ARRAY_SIZE(default_vlan);
  363. + break;
  364. + case SWITCH_CMD_LIST_PORT:
  365. + alist = &dev->attr_port;
  366. + def_list = default_port;
  367. + def_active = &dev->def_port;
  368. + n_def = ARRAY_SIZE(default_port);
  369. + break;
  370. + default:
  371. + WARN_ON(1);
  372. + goto out;
  373. + }
  374. +
  375. + memset(&cb, 0, sizeof(cb));
  376. + cb.info = info;
  377. + cb.fill = swconfig_dump_attr;
  378. + for (i = 0; i < alist->n_attr; i++) {
  379. + if (alist->attr[i].disabled)
  380. + continue;
  381. + cb.args[0] = i;
  382. + err = swconfig_send_multipart(&cb, &alist->attr[i]);
  383. + if (err < 0)
  384. + goto error;
  385. + }
  386. +
  387. + /* defaults */
  388. + for (i = 0; i < n_def; i++) {
  389. + if (!test_bit(i, def_active))
  390. + continue;
  391. + cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
  392. + err = swconfig_send_multipart(&cb, &def_list[i]);
  393. + if (err < 0)
  394. + goto error;
  395. + }
  396. + swconfig_put_dev(dev);
  397. +
  398. + if (!cb.msg)
  399. + return 0;
  400. +
  401. + return genlmsg_unicast(cb.msg, info->snd_pid);
  402. +
  403. +error:
  404. + if (cb.msg)
  405. + nlmsg_free(cb.msg);
  406. +out:
  407. + swconfig_put_dev(dev);
  408. + return err;
  409. +}
  410. +
  411. +static struct switch_attr *
  412. +swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
  413. + struct switch_val *val)
  414. +{
  415. + struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  416. + const struct switch_attrlist *alist;
  417. + struct switch_attr *attr = NULL;
  418. + int attr_id;
  419. +
  420. + /* defaults */
  421. + struct switch_attr *def_list;
  422. + unsigned long *def_active;
  423. + int n_def;
  424. +
  425. + if (!info->attrs[SWITCH_ATTR_OP_ID])
  426. + goto done;
  427. +
  428. + switch(hdr->cmd) {
  429. + case SWITCH_CMD_SET_GLOBAL:
  430. + case SWITCH_CMD_GET_GLOBAL:
  431. + alist = &dev->attr_global;
  432. + def_list = default_global;
  433. + def_active = &dev->def_global;
  434. + n_def = ARRAY_SIZE(default_global);
  435. + break;
  436. + case SWITCH_CMD_SET_VLAN:
  437. + case SWITCH_CMD_GET_VLAN:
  438. + alist = &dev->attr_vlan;
  439. + def_list = default_vlan;
  440. + def_active = &dev->def_vlan;
  441. + n_def = ARRAY_SIZE(default_vlan);
  442. + if (!info->attrs[SWITCH_ATTR_OP_VLAN])
  443. + goto done;
  444. + val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
  445. + break;
  446. + case SWITCH_CMD_SET_PORT:
  447. + case SWITCH_CMD_GET_PORT:
  448. + alist = &dev->attr_port;
  449. + def_list = default_port;
  450. + def_active = &dev->def_port;
  451. + n_def = ARRAY_SIZE(default_port);
  452. + if (!info->attrs[SWITCH_ATTR_OP_PORT])
  453. + goto done;
  454. + val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
  455. + break;
  456. + default:
  457. + WARN_ON(1);
  458. + goto done;
  459. + }
  460. +
  461. + if (!alist)
  462. + goto done;
  463. +
  464. + attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
  465. + if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
  466. + attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
  467. + if (attr_id >= n_def)
  468. + goto done;
  469. + if (!test_bit(attr_id, def_active))
  470. + goto done;
  471. + attr = &def_list[attr_id];
  472. + } else {
  473. + if (attr_id >= alist->n_attr)
  474. + goto done;
  475. + attr = &alist->attr[attr_id];
  476. + }
  477. +
  478. + if (attr->disabled)
  479. + attr = NULL;
  480. +
  481. +done:
  482. + if (!attr)
  483. + DPRINTF("attribute lookup failed\n");
  484. + val->attr = attr;
  485. + return attr;
  486. +}
  487. +
  488. +static int
  489. +swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
  490. + struct switch_val *val, int max)
  491. +{
  492. + struct nlattr *nla;
  493. + int rem;
  494. +
  495. + val->len = 0;
  496. + nla_for_each_nested(nla, head, rem) {
  497. + struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
  498. + struct switch_port *port = &val->value.ports[val->len];
  499. +
  500. + if (val->len >= max)
  501. + return -EINVAL;
  502. +
  503. + if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
  504. + port_policy))
  505. + return -EINVAL;
  506. +
  507. + if (!tb[SWITCH_PORT_ID])
  508. + return -EINVAL;
  509. +
  510. + port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
  511. + if (tb[SWITCH_PORT_FLAG_TAGGED])
  512. + port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
  513. + val->len++;
  514. + }
  515. +
  516. + return 0;
  517. +}
  518. +
  519. +static int
  520. +swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
  521. +{
  522. + struct switch_attr *attr;
  523. + struct switch_dev *dev;
  524. + struct switch_val val;
  525. + int err = -EINVAL;
  526. +
  527. + dev = swconfig_get_dev(info);
  528. + if (!dev)
  529. + return -EINVAL;
  530. +
  531. + memset(&val, 0, sizeof(val));
  532. + attr = swconfig_lookup_attr(dev, info, &val);
  533. + if (!attr || !attr->set)
  534. + goto error;
  535. +
  536. + val.attr = attr;
  537. + switch(attr->type) {
  538. + case SWITCH_TYPE_NOVAL:
  539. + break;
  540. + case SWITCH_TYPE_INT:
  541. + if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
  542. + goto error;
  543. + val.value.i =
  544. + nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
  545. + break;
  546. + case SWITCH_TYPE_STRING:
  547. + if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
  548. + goto error;
  549. + val.value.s =
  550. + nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
  551. + break;
  552. + case SWITCH_TYPE_PORTS:
  553. + val.value.ports = dev->portbuf;
  554. + memset(dev->portbuf, 0,
  555. + sizeof(struct switch_port) * dev->ports);
  556. +
  557. + /* TODO: implement multipart? */
  558. + if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
  559. + err = swconfig_parse_ports(skb,
  560. + info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
  561. + if (err < 0)
  562. + goto error;
  563. + } else {
  564. + val.len = 0;
  565. + err = 0;
  566. + }
  567. + break;
  568. + default:
  569. + goto error;
  570. + }
  571. +
  572. + err = attr->set(dev, attr, &val);
  573. +error:
  574. + swconfig_put_dev(dev);
  575. + return err;
  576. +}
  577. +
  578. +static int
  579. +swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
  580. +{
  581. + if (cb->nest[0])
  582. + nla_nest_end(cb->msg, cb->nest[0]);
  583. + return 0;
  584. +}
  585. +
  586. +static int
  587. +swconfig_send_port(struct swconfig_callback *cb, void *arg)
  588. +{
  589. + const struct switch_port *port = arg;
  590. + struct nlattr *p = NULL;
  591. +
  592. + if (!cb->nest[0]) {
  593. + cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
  594. + if (!cb->nest[0])
  595. + return -1;
  596. + }
  597. +
  598. + p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
  599. + if (!p)
  600. + goto error;
  601. +
  602. + NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
  603. + if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
  604. + NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
  605. +
  606. + nla_nest_end(cb->msg, p);
  607. + return 0;
  608. +
  609. +nla_put_failure:
  610. + nla_nest_cancel(cb->msg, p);
  611. +error:
  612. + nla_nest_cancel(cb->msg, cb->nest[0]);
  613. + return -1;
  614. +}
  615. +
  616. +static int
  617. +swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
  618. + const struct switch_val *val)
  619. +{
  620. + struct swconfig_callback cb;
  621. + int err = 0;
  622. + int i;
  623. +
  624. + if (!val->value.ports)
  625. + return -EINVAL;
  626. +
  627. + memset(&cb, 0, sizeof(cb));
  628. + cb.cmd = attr;
  629. + cb.msg = *msg;
  630. + cb.info = info;
  631. + cb.fill = swconfig_send_port;
  632. + cb.close = swconfig_close_portlist;
  633. +
  634. + cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
  635. + for (i = 0; i < val->len; i++) {
  636. + err = swconfig_send_multipart(&cb, &val->value.ports[i]);
  637. + if (err)
  638. + goto done;
  639. + }
  640. + err = val->len;
  641. + swconfig_close_portlist(&cb, NULL);
  642. + *msg = cb.msg;
  643. +
  644. +done:
  645. + return err;
  646. +}
  647. +
  648. +static int
  649. +swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
  650. +{
  651. + struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  652. + struct switch_attr *attr;
  653. + struct switch_dev *dev;
  654. + struct sk_buff *msg = NULL;
  655. + struct switch_val val;
  656. + int err = -EINVAL;
  657. + int cmd = hdr->cmd;
  658. +
  659. + dev = swconfig_get_dev(info);
  660. + if (!dev)
  661. + return -EINVAL;
  662. +
  663. + memset(&val, 0, sizeof(val));
  664. + attr = swconfig_lookup_attr(dev, info, &val);
  665. + if (!attr || !attr->get)
  666. + goto error_dev;
  667. +
  668. + if (attr->type == SWITCH_TYPE_PORTS) {
  669. + val.value.ports = dev->portbuf;
  670. + memset(dev->portbuf, 0,
  671. + sizeof(struct switch_port) * dev->ports);
  672. + }
  673. +
  674. + err = attr->get(dev, attr, &val);
  675. + if (err)
  676. + goto error;
  677. +
  678. + msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  679. + if (!msg)
  680. + goto error;
  681. +
  682. + hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
  683. + 0, cmd);
  684. + if (IS_ERR(hdr))
  685. + goto nla_put_failure;
  686. +
  687. + switch(attr->type) {
  688. + case SWITCH_TYPE_INT:
  689. + NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
  690. + break;
  691. + case SWITCH_TYPE_STRING:
  692. + NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
  693. + break;
  694. + case SWITCH_TYPE_PORTS:
  695. + err = swconfig_send_ports(&msg, info,
  696. + SWITCH_ATTR_OP_VALUE_PORTS, &val);
  697. + if (err < 0)
  698. + goto nla_put_failure;
  699. + break;
  700. + default:
  701. + DPRINTF("invalid type in attribute\n");
  702. + err = -EINVAL;
  703. + goto error;
  704. + }
  705. + err = genlmsg_end(msg, hdr);
  706. + if (err < 0)
  707. + goto nla_put_failure;
  708. +
  709. + swconfig_put_dev(dev);
  710. + return genlmsg_unicast(msg, info->snd_pid);
  711. +
  712. +nla_put_failure:
  713. + if (msg)
  714. + nlmsg_free(msg);
  715. +error_dev:
  716. + swconfig_put_dev(dev);
  717. +error:
  718. + if (!err)
  719. + err = -ENOMEM;
  720. + return err;
  721. +}
  722. +
  723. +static int
  724. +swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
  725. + const struct switch_dev *dev)
  726. +{
  727. + void *hdr;
  728. +
  729. + hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
  730. + SWITCH_CMD_NEW_ATTR);
  731. + if (IS_ERR(hdr))
  732. + return -1;
  733. +
  734. + NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
  735. + NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
  736. + NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
  737. + NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
  738. + NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
  739. +
  740. + return genlmsg_end(msg, hdr);
  741. +nla_put_failure:
  742. + genlmsg_cancel(msg, hdr);
  743. + return -EMSGSIZE;
  744. +}
  745. +
  746. +static int swconfig_dump_switches(struct sk_buff *skb,
  747. + struct netlink_callback *cb)
  748. +{
  749. + struct switch_dev *dev;
  750. + int start = cb->args[0];
  751. + int idx = 0;
  752. +
  753. + swconfig_lock();
  754. + list_for_each_entry(dev, &swdevs, dev_list) {
  755. + if (++idx <= start)
  756. + continue;
  757. + if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
  758. + cb->nlh->nlmsg_seq, NLM_F_MULTI,
  759. + dev) < 0)
  760. + break;
  761. + }
  762. + swconfig_unlock();
  763. + cb->args[0] = idx;
  764. +
  765. + return skb->len;
  766. +}
  767. +
  768. +static int
  769. +swconfig_done(struct netlink_callback *cb)
  770. +{
  771. + return 0;
  772. +}
  773. +
  774. +static struct genl_ops swconfig_ops[] = {
  775. + {
  776. + .cmd = SWITCH_CMD_LIST_GLOBAL,
  777. + .doit = swconfig_list_attrs,
  778. + .policy = switch_policy,
  779. + },
  780. + {
  781. + .cmd = SWITCH_CMD_LIST_VLAN,
  782. + .doit = swconfig_list_attrs,
  783. + .policy = switch_policy,
  784. + },
  785. + {
  786. + .cmd = SWITCH_CMD_LIST_PORT,
  787. + .doit = swconfig_list_attrs,
  788. + .policy = switch_policy,
  789. + },
  790. + {
  791. + .cmd = SWITCH_CMD_GET_GLOBAL,
  792. + .doit = swconfig_get_attr,
  793. + .policy = switch_policy,
  794. + },
  795. + {
  796. + .cmd = SWITCH_CMD_GET_VLAN,
  797. + .doit = swconfig_get_attr,
  798. + .policy = switch_policy,
  799. + },
  800. + {
  801. + .cmd = SWITCH_CMD_GET_PORT,
  802. + .doit = swconfig_get_attr,
  803. + .policy = switch_policy,
  804. + },
  805. + {
  806. + .cmd = SWITCH_CMD_SET_GLOBAL,
  807. + .doit = swconfig_set_attr,
  808. + .policy = switch_policy,
  809. + },
  810. + {
  811. + .cmd = SWITCH_CMD_SET_VLAN,
  812. + .doit = swconfig_set_attr,
  813. + .policy = switch_policy,
  814. + },
  815. + {
  816. + .cmd = SWITCH_CMD_SET_PORT,
  817. + .doit = swconfig_set_attr,
  818. + .policy = switch_policy,
  819. + },
  820. + {
  821. + .cmd = SWITCH_CMD_GET_SWITCH,
  822. + .dumpit = swconfig_dump_switches,
  823. + .policy = switch_policy,
  824. + .done = swconfig_done,
  825. + }
  826. +};
  827. +
  828. +int
  829. +register_switch(struct switch_dev *dev, struct net_device *netdev)
  830. +{
  831. + INIT_LIST_HEAD(&dev->dev_list);
  832. + if (netdev) {
  833. + dev->netdev = netdev;
  834. + if (!dev->devname)
  835. + dev->devname = netdev->name;
  836. + }
  837. + BUG_ON(!dev->devname);
  838. +
  839. + if (dev->ports > 0) {
  840. + dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
  841. + GFP_KERNEL);
  842. + if (!dev->portbuf)
  843. + return -ENOMEM;
  844. + }
  845. + dev->id = ++swdev_id;
  846. + swconfig_defaults_init(dev);
  847. + spin_lock_init(&dev->lock);
  848. + swconfig_lock();
  849. + list_add(&dev->dev_list, &swdevs);
  850. + swconfig_unlock();
  851. +
  852. + return 0;
  853. +}
  854. +EXPORT_SYMBOL_GPL(register_switch);
  855. +
  856. +void
  857. +unregister_switch(struct switch_dev *dev)
  858. +{
  859. + kfree(dev->portbuf);
  860. + spin_lock(&dev->lock);
  861. + swconfig_lock();
  862. + list_del(&dev->dev_list);
  863. + swconfig_unlock();
  864. +}
  865. +EXPORT_SYMBOL_GPL(unregister_switch);
  866. +
  867. +
  868. +static int __init
  869. +swconfig_init(void)
  870. +{
  871. + int i, err;
  872. +
  873. + INIT_LIST_HEAD(&swdevs);
  874. + err = genl_register_family(&switch_fam);
  875. + if (err)
  876. + return err;
  877. +
  878. + for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
  879. + err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
  880. + if (err)
  881. + goto unregister;
  882. + }
  883. +
  884. + return 0;
  885. +
  886. +unregister:
  887. + genl_unregister_family(&switch_fam);
  888. + return err;
  889. +}
  890. +
  891. +static void __exit
  892. +swconfig_exit(void)
  893. +{
  894. + genl_unregister_family(&switch_fam);
  895. +}
  896. +
  897. +module_init(swconfig_init);
  898. +module_exit(swconfig_exit);
  899. +
  900. diff -Nur linux-2.6.30.orig/include/linux/switch.h linux-2.6.30/include/linux/switch.h
  901. --- linux-2.6.30.orig/include/linux/switch.h 1970-01-01 01:00:00.000000000 +0100
  902. +++ linux-2.6.30/include/linux/switch.h 2009-06-11 09:22:50.000000000 +0200
  903. @@ -0,0 +1,168 @@
  904. +/*
  905. + * switch.h: Switch configuration API
  906. + *
  907. + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  908. + *
  909. + * This program is free software; you can redistribute it and/or
  910. + * modify it under the terms of the GNU General Public License
  911. + * as published by the Free Software Foundation; either version 2
  912. + * of the License, or (at your option) any later version.
  913. + *
  914. + * This program is distributed in the hope that it will be useful,
  915. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  916. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  917. + * GNU General Public License for more details.
  918. + */
  919. +
  920. +#ifndef __LINUX_SWITCH_H
  921. +#define __LINUX_SWITCH_H
  922. +
  923. +#include <linux/types.h>
  924. +#include <linux/netdevice.h>
  925. +#include <linux/netlink.h>
  926. +#include <linux/genetlink.h>
  927. +#ifndef __KERNEL__
  928. +#include <netlink/netlink.h>
  929. +#include <netlink/genl/genl.h>
  930. +#include <netlink/genl/ctrl.h>
  931. +#else
  932. +#include <net/genetlink.h>
  933. +#endif
  934. +
  935. +/* main attributes */
  936. +enum {
  937. + SWITCH_ATTR_UNSPEC,
  938. + /* global */
  939. + SWITCH_ATTR_TYPE,
  940. + /* device */
  941. + SWITCH_ATTR_ID,
  942. + SWITCH_ATTR_NAME,
  943. + SWITCH_ATTR_DEV_NAME,
  944. + SWITCH_ATTR_VLANS,
  945. + SWITCH_ATTR_PORTS,
  946. + /* attributes */
  947. + SWITCH_ATTR_OP_ID,
  948. + SWITCH_ATTR_OP_TYPE,
  949. + SWITCH_ATTR_OP_NAME,
  950. + SWITCH_ATTR_OP_PORT,
  951. + SWITCH_ATTR_OP_VLAN,
  952. + SWITCH_ATTR_OP_VALUE_INT,
  953. + SWITCH_ATTR_OP_VALUE_STR,
  954. + SWITCH_ATTR_OP_VALUE_PORTS,
  955. + SWITCH_ATTR_OP_DESCRIPTION,
  956. + /* port lists */
  957. + SWITCH_ATTR_PORT,
  958. + SWITCH_ATTR_MAX
  959. +};
  960. +
  961. +/* commands */
  962. +enum {
  963. + SWITCH_CMD_UNSPEC,
  964. + SWITCH_CMD_GET_SWITCH,
  965. + SWITCH_CMD_NEW_ATTR,
  966. + SWITCH_CMD_LIST_GLOBAL,
  967. + SWITCH_CMD_GET_GLOBAL,
  968. + SWITCH_CMD_SET_GLOBAL,
  969. + SWITCH_CMD_LIST_PORT,
  970. + SWITCH_CMD_GET_PORT,
  971. + SWITCH_CMD_SET_PORT,
  972. + SWITCH_CMD_LIST_VLAN,
  973. + SWITCH_CMD_GET_VLAN,
  974. + SWITCH_CMD_SET_VLAN
  975. +};
  976. +
  977. +/* data types */
  978. +enum switch_val_type {
  979. + SWITCH_TYPE_UNSPEC,
  980. + SWITCH_TYPE_INT,
  981. + SWITCH_TYPE_STRING,
  982. + SWITCH_TYPE_PORTS,
  983. + SWITCH_TYPE_NOVAL,
  984. +};
  985. +
  986. +/* port nested attributes */
  987. +enum {
  988. + SWITCH_PORT_UNSPEC,
  989. + SWITCH_PORT_ID,
  990. + SWITCH_PORT_FLAG_TAGGED,
  991. + SWITCH_PORT_ATTR_MAX
  992. +};
  993. +
  994. +#define SWITCH_ATTR_DEFAULTS_OFFSET 0x1000
  995. +
  996. +#ifdef __KERNEL__
  997. +
  998. +struct switch_dev;
  999. +struct switch_op;
  1000. +struct switch_val;
  1001. +struct switch_attr;
  1002. +struct switch_attrlist;
  1003. +
  1004. +int register_switch(struct switch_dev *dev, struct net_device *netdev);
  1005. +void unregister_switch(struct switch_dev *dev);
  1006. +
  1007. +struct switch_attrlist {
  1008. + /* filled in by the driver */
  1009. + int n_attr;
  1010. + struct switch_attr *attr;
  1011. +};
  1012. +
  1013. +
  1014. +struct switch_dev {
  1015. + int id;
  1016. + void *priv;
  1017. + const char *name;
  1018. +
  1019. + /* NB: either devname or netdev must be set */
  1020. + const char *devname;
  1021. + struct net_device *netdev;
  1022. +
  1023. + int ports;
  1024. + int vlans;
  1025. + int cpu_port;
  1026. + struct switch_attrlist attr_global, attr_port, attr_vlan;
  1027. +
  1028. + spinlock_t lock;
  1029. + struct switch_port *portbuf;
  1030. + struct list_head dev_list;
  1031. + unsigned long def_global, def_port, def_vlan;
  1032. +
  1033. + int (*get_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
  1034. + int (*set_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
  1035. + int (*apply_config)(struct switch_dev *dev);
  1036. +};
  1037. +
  1038. +struct switch_port {
  1039. + u32 id;
  1040. + u32 flags;
  1041. +};
  1042. +
  1043. +struct switch_val {
  1044. + struct switch_attr *attr;
  1045. + int port_vlan;
  1046. + int len;
  1047. + union {
  1048. + const char *s;
  1049. + u32 i;
  1050. + struct switch_port *ports;
  1051. + } value;
  1052. +};
  1053. +
  1054. +struct switch_attr {
  1055. + int disabled;
  1056. + int type;
  1057. + const char *name;
  1058. + const char *description;
  1059. +
  1060. + int (*set)(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val);
  1061. + int (*get)(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val);
  1062. +
  1063. + /* for driver internal use */
  1064. + int id;
  1065. + int ofs;
  1066. + int max;
  1067. +};
  1068. +
  1069. +#endif
  1070. +
  1071. +#endif