swconfig.c 24 KB

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