br_stp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Spanning tree protocol; generic parts
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/rculist.h>
  15. #include <net/switchdev.h>
  16. #include "br_private.h"
  17. #include "br_private_stp.h"
  18. /* since time values in bpdu are in jiffies and then scaled (1/256)
  19. * before sending, make sure that is at least one STP tick.
  20. */
  21. #define MESSAGE_AGE_INCR ((HZ / 256) + 1)
  22. static const char *const br_port_state_names[] = {
  23. [BR_STATE_DISABLED] = "disabled",
  24. [BR_STATE_LISTENING] = "listening",
  25. [BR_STATE_LEARNING] = "learning",
  26. [BR_STATE_FORWARDING] = "forwarding",
  27. [BR_STATE_BLOCKING] = "blocking",
  28. };
  29. void br_set_state(struct net_bridge_port *p, unsigned int state)
  30. {
  31. struct switchdev_attr attr = {
  32. .orig_dev = p->dev,
  33. .id = SWITCHDEV_ATTR_ID_PORT_STP_STATE,
  34. .flags = SWITCHDEV_F_DEFER,
  35. .u.stp_state = state,
  36. };
  37. int err;
  38. p->state = state;
  39. err = switchdev_port_attr_set(p->dev, &attr);
  40. if (err && err != -EOPNOTSUPP)
  41. br_warn(p->br, "error setting offload STP state on port %u(%s)\n",
  42. (unsigned int) p->port_no, p->dev->name);
  43. else
  44. br_info(p->br, "port %u(%s) entered %s state\n",
  45. (unsigned int) p->port_no, p->dev->name,
  46. br_port_state_names[p->state]);
  47. }
  48. /* called under bridge lock */
  49. struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
  50. {
  51. struct net_bridge_port *p;
  52. list_for_each_entry_rcu(p, &br->port_list, list) {
  53. if (p->port_no == port_no)
  54. return p;
  55. }
  56. return NULL;
  57. }
  58. /* called under bridge lock */
  59. static int br_should_become_root_port(const struct net_bridge_port *p,
  60. u16 root_port)
  61. {
  62. struct net_bridge *br;
  63. struct net_bridge_port *rp;
  64. int t;
  65. br = p->br;
  66. if (p->state == BR_STATE_DISABLED ||
  67. br_is_designated_port(p))
  68. return 0;
  69. if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
  70. return 0;
  71. if (!root_port)
  72. return 1;
  73. rp = br_get_port(br, root_port);
  74. t = memcmp(&p->designated_root, &rp->designated_root, 8);
  75. if (t < 0)
  76. return 1;
  77. else if (t > 0)
  78. return 0;
  79. if (p->designated_cost + p->path_cost <
  80. rp->designated_cost + rp->path_cost)
  81. return 1;
  82. else if (p->designated_cost + p->path_cost >
  83. rp->designated_cost + rp->path_cost)
  84. return 0;
  85. t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
  86. if (t < 0)
  87. return 1;
  88. else if (t > 0)
  89. return 0;
  90. if (p->designated_port < rp->designated_port)
  91. return 1;
  92. else if (p->designated_port > rp->designated_port)
  93. return 0;
  94. if (p->port_id < rp->port_id)
  95. return 1;
  96. return 0;
  97. }
  98. static void br_root_port_block(const struct net_bridge *br,
  99. struct net_bridge_port *p)
  100. {
  101. br_notice(br, "port %u(%s) tried to become root port (blocked)",
  102. (unsigned int) p->port_no, p->dev->name);
  103. br_set_state(p, BR_STATE_LISTENING);
  104. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  105. if (br->forward_delay > 0)
  106. mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
  107. }
  108. /* called under bridge lock */
  109. static void br_root_selection(struct net_bridge *br)
  110. {
  111. struct net_bridge_port *p;
  112. u16 root_port = 0;
  113. list_for_each_entry(p, &br->port_list, list) {
  114. if (!br_should_become_root_port(p, root_port))
  115. continue;
  116. if (p->flags & BR_ROOT_BLOCK)
  117. br_root_port_block(br, p);
  118. else
  119. root_port = p->port_no;
  120. }
  121. br->root_port = root_port;
  122. if (!root_port) {
  123. br->designated_root = br->bridge_id;
  124. br->root_path_cost = 0;
  125. } else {
  126. p = br_get_port(br, root_port);
  127. br->designated_root = p->designated_root;
  128. br->root_path_cost = p->designated_cost + p->path_cost;
  129. }
  130. }
  131. /* called under bridge lock */
  132. void br_become_root_bridge(struct net_bridge *br)
  133. {
  134. br->max_age = br->bridge_max_age;
  135. br->hello_time = br->bridge_hello_time;
  136. br->forward_delay = br->bridge_forward_delay;
  137. br_topology_change_detection(br);
  138. del_timer(&br->tcn_timer);
  139. if (br->dev->flags & IFF_UP) {
  140. br_config_bpdu_generation(br);
  141. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  142. }
  143. }
  144. /* called under bridge lock */
  145. void br_transmit_config(struct net_bridge_port *p)
  146. {
  147. struct br_config_bpdu bpdu;
  148. struct net_bridge *br;
  149. if (timer_pending(&p->hold_timer)) {
  150. p->config_pending = 1;
  151. return;
  152. }
  153. br = p->br;
  154. bpdu.topology_change = br->topology_change;
  155. bpdu.topology_change_ack = p->topology_change_ack;
  156. bpdu.root = br->designated_root;
  157. bpdu.root_path_cost = br->root_path_cost;
  158. bpdu.bridge_id = br->bridge_id;
  159. bpdu.port_id = p->port_id;
  160. if (br_is_root_bridge(br))
  161. bpdu.message_age = 0;
  162. else {
  163. struct net_bridge_port *root
  164. = br_get_port(br, br->root_port);
  165. bpdu.message_age = (jiffies - root->designated_age)
  166. + MESSAGE_AGE_INCR;
  167. }
  168. bpdu.max_age = br->max_age;
  169. bpdu.hello_time = br->hello_time;
  170. bpdu.forward_delay = br->forward_delay;
  171. if (bpdu.message_age < br->max_age) {
  172. br_send_config_bpdu(p, &bpdu);
  173. p->topology_change_ack = 0;
  174. p->config_pending = 0;
  175. if (p->br->stp_enabled == BR_KERNEL_STP)
  176. mod_timer(&p->hold_timer,
  177. round_jiffies(jiffies + BR_HOLD_TIME));
  178. }
  179. }
  180. /* called under bridge lock */
  181. static void br_record_config_information(struct net_bridge_port *p,
  182. const struct br_config_bpdu *bpdu)
  183. {
  184. p->designated_root = bpdu->root;
  185. p->designated_cost = bpdu->root_path_cost;
  186. p->designated_bridge = bpdu->bridge_id;
  187. p->designated_port = bpdu->port_id;
  188. p->designated_age = jiffies - bpdu->message_age;
  189. mod_timer(&p->message_age_timer, jiffies
  190. + (bpdu->max_age - bpdu->message_age));
  191. }
  192. /* called under bridge lock */
  193. static void br_record_config_timeout_values(struct net_bridge *br,
  194. const struct br_config_bpdu *bpdu)
  195. {
  196. br->max_age = bpdu->max_age;
  197. br->hello_time = bpdu->hello_time;
  198. br->forward_delay = bpdu->forward_delay;
  199. __br_set_topology_change(br, bpdu->topology_change);
  200. }
  201. /* called under bridge lock */
  202. void br_transmit_tcn(struct net_bridge *br)
  203. {
  204. struct net_bridge_port *p;
  205. p = br_get_port(br, br->root_port);
  206. if (p)
  207. br_send_tcn_bpdu(p);
  208. else
  209. br_notice(br, "root port %u not found for topology notice\n",
  210. br->root_port);
  211. }
  212. /* called under bridge lock */
  213. static int br_should_become_designated_port(const struct net_bridge_port *p)
  214. {
  215. struct net_bridge *br;
  216. int t;
  217. br = p->br;
  218. if (br_is_designated_port(p))
  219. return 1;
  220. if (memcmp(&p->designated_root, &br->designated_root, 8))
  221. return 1;
  222. if (br->root_path_cost < p->designated_cost)
  223. return 1;
  224. else if (br->root_path_cost > p->designated_cost)
  225. return 0;
  226. t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
  227. if (t < 0)
  228. return 1;
  229. else if (t > 0)
  230. return 0;
  231. if (p->port_id < p->designated_port)
  232. return 1;
  233. return 0;
  234. }
  235. /* called under bridge lock */
  236. static void br_designated_port_selection(struct net_bridge *br)
  237. {
  238. struct net_bridge_port *p;
  239. list_for_each_entry(p, &br->port_list, list) {
  240. if (p->state != BR_STATE_DISABLED &&
  241. br_should_become_designated_port(p))
  242. br_become_designated_port(p);
  243. }
  244. }
  245. /* called under bridge lock */
  246. static int br_supersedes_port_info(const struct net_bridge_port *p,
  247. const struct br_config_bpdu *bpdu)
  248. {
  249. int t;
  250. t = memcmp(&bpdu->root, &p->designated_root, 8);
  251. if (t < 0)
  252. return 1;
  253. else if (t > 0)
  254. return 0;
  255. if (bpdu->root_path_cost < p->designated_cost)
  256. return 1;
  257. else if (bpdu->root_path_cost > p->designated_cost)
  258. return 0;
  259. t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
  260. if (t < 0)
  261. return 1;
  262. else if (t > 0)
  263. return 0;
  264. if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
  265. return 1;
  266. if (bpdu->port_id <= p->designated_port)
  267. return 1;
  268. return 0;
  269. }
  270. /* called under bridge lock */
  271. static void br_topology_change_acknowledged(struct net_bridge *br)
  272. {
  273. br->topology_change_detected = 0;
  274. del_timer(&br->tcn_timer);
  275. }
  276. /* called under bridge lock */
  277. void br_topology_change_detection(struct net_bridge *br)
  278. {
  279. int isroot = br_is_root_bridge(br);
  280. if (br->stp_enabled != BR_KERNEL_STP)
  281. return;
  282. br_info(br, "topology change detected, %s\n",
  283. isroot ? "propagating" : "sending tcn bpdu");
  284. if (isroot) {
  285. __br_set_topology_change(br, 1);
  286. mod_timer(&br->topology_change_timer, jiffies
  287. + br->bridge_forward_delay + br->bridge_max_age);
  288. } else if (!br->topology_change_detected) {
  289. br_transmit_tcn(br);
  290. mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
  291. }
  292. br->topology_change_detected = 1;
  293. }
  294. /* called under bridge lock */
  295. void br_config_bpdu_generation(struct net_bridge *br)
  296. {
  297. struct net_bridge_port *p;
  298. list_for_each_entry(p, &br->port_list, list) {
  299. if (p->state != BR_STATE_DISABLED &&
  300. br_is_designated_port(p))
  301. br_transmit_config(p);
  302. }
  303. }
  304. /* called under bridge lock */
  305. static void br_reply(struct net_bridge_port *p)
  306. {
  307. br_transmit_config(p);
  308. }
  309. /* called under bridge lock */
  310. void br_configuration_update(struct net_bridge *br)
  311. {
  312. br_root_selection(br);
  313. br_designated_port_selection(br);
  314. }
  315. /* called under bridge lock */
  316. void br_become_designated_port(struct net_bridge_port *p)
  317. {
  318. struct net_bridge *br;
  319. br = p->br;
  320. p->designated_root = br->designated_root;
  321. p->designated_cost = br->root_path_cost;
  322. p->designated_bridge = br->bridge_id;
  323. p->designated_port = p->port_id;
  324. }
  325. /* called under bridge lock */
  326. static void br_make_blocking(struct net_bridge_port *p)
  327. {
  328. if (p->state != BR_STATE_DISABLED &&
  329. p->state != BR_STATE_BLOCKING) {
  330. if (p->state == BR_STATE_FORWARDING ||
  331. p->state == BR_STATE_LEARNING)
  332. br_topology_change_detection(p->br);
  333. br_set_state(p, BR_STATE_BLOCKING);
  334. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  335. del_timer(&p->forward_delay_timer);
  336. }
  337. }
  338. /* called under bridge lock */
  339. static void br_make_forwarding(struct net_bridge_port *p)
  340. {
  341. struct net_bridge *br = p->br;
  342. if (p->state != BR_STATE_BLOCKING)
  343. return;
  344. if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
  345. br_set_state(p, BR_STATE_FORWARDING);
  346. br_topology_change_detection(br);
  347. del_timer(&p->forward_delay_timer);
  348. } else if (br->stp_enabled == BR_KERNEL_STP)
  349. br_set_state(p, BR_STATE_LISTENING);
  350. else
  351. br_set_state(p, BR_STATE_LEARNING);
  352. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  353. if (br->forward_delay != 0)
  354. mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
  355. }
  356. /* called under bridge lock */
  357. void br_port_state_selection(struct net_bridge *br)
  358. {
  359. struct net_bridge_port *p;
  360. unsigned int liveports = 0;
  361. list_for_each_entry(p, &br->port_list, list) {
  362. if (p->state == BR_STATE_DISABLED)
  363. continue;
  364. /* Don't change port states if userspace is handling STP */
  365. if (br->stp_enabled != BR_USER_STP) {
  366. if (p->port_no == br->root_port) {
  367. p->config_pending = 0;
  368. p->topology_change_ack = 0;
  369. br_make_forwarding(p);
  370. } else if (br_is_designated_port(p)) {
  371. del_timer(&p->message_age_timer);
  372. br_make_forwarding(p);
  373. } else {
  374. p->config_pending = 0;
  375. p->topology_change_ack = 0;
  376. br_make_blocking(p);
  377. }
  378. }
  379. if (p->state != BR_STATE_BLOCKING)
  380. br_multicast_enable_port(p);
  381. /* Multicast is not disabled for the port when it goes in
  382. * blocking state because the timers will expire and stop by
  383. * themselves without sending more queries.
  384. */
  385. if (p->state == BR_STATE_FORWARDING)
  386. ++liveports;
  387. }
  388. if (liveports == 0)
  389. netif_carrier_off(br->dev);
  390. else
  391. netif_carrier_on(br->dev);
  392. }
  393. /* called under bridge lock */
  394. static void br_topology_change_acknowledge(struct net_bridge_port *p)
  395. {
  396. p->topology_change_ack = 1;
  397. br_transmit_config(p);
  398. }
  399. /* called under bridge lock */
  400. void br_received_config_bpdu(struct net_bridge_port *p,
  401. const struct br_config_bpdu *bpdu)
  402. {
  403. struct net_bridge *br;
  404. int was_root;
  405. br = p->br;
  406. was_root = br_is_root_bridge(br);
  407. if (br_supersedes_port_info(p, bpdu)) {
  408. br_record_config_information(p, bpdu);
  409. br_configuration_update(br);
  410. br_port_state_selection(br);
  411. if (!br_is_root_bridge(br) && was_root) {
  412. del_timer(&br->hello_timer);
  413. if (br->topology_change_detected) {
  414. del_timer(&br->topology_change_timer);
  415. br_transmit_tcn(br);
  416. mod_timer(&br->tcn_timer,
  417. jiffies + br->bridge_hello_time);
  418. }
  419. }
  420. if (p->port_no == br->root_port) {
  421. br_record_config_timeout_values(br, bpdu);
  422. br_config_bpdu_generation(br);
  423. if (bpdu->topology_change_ack)
  424. br_topology_change_acknowledged(br);
  425. }
  426. } else if (br_is_designated_port(p)) {
  427. br_reply(p);
  428. }
  429. }
  430. /* called under bridge lock */
  431. void br_received_tcn_bpdu(struct net_bridge_port *p)
  432. {
  433. if (br_is_designated_port(p)) {
  434. br_info(p->br, "port %u(%s) received tcn bpdu\n",
  435. (unsigned int) p->port_no, p->dev->name);
  436. br_topology_change_detection(p->br);
  437. br_topology_change_acknowledge(p);
  438. }
  439. }
  440. /* Change bridge STP parameter */
  441. int br_set_hello_time(struct net_bridge *br, unsigned long val)
  442. {
  443. unsigned long t = clock_t_to_jiffies(val);
  444. if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
  445. return -ERANGE;
  446. spin_lock_bh(&br->lock);
  447. br->bridge_hello_time = t;
  448. if (br_is_root_bridge(br))
  449. br->hello_time = br->bridge_hello_time;
  450. spin_unlock_bh(&br->lock);
  451. return 0;
  452. }
  453. int br_set_max_age(struct net_bridge *br, unsigned long val)
  454. {
  455. unsigned long t = clock_t_to_jiffies(val);
  456. if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
  457. return -ERANGE;
  458. spin_lock_bh(&br->lock);
  459. br->bridge_max_age = t;
  460. if (br_is_root_bridge(br))
  461. br->max_age = br->bridge_max_age;
  462. spin_unlock_bh(&br->lock);
  463. return 0;
  464. }
  465. /* called under bridge lock */
  466. int __set_ageing_time(struct net_device *dev, unsigned long t)
  467. {
  468. struct switchdev_attr attr = {
  469. .orig_dev = dev,
  470. .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
  471. .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
  472. .u.ageing_time = jiffies_to_clock_t(t),
  473. };
  474. int err;
  475. err = switchdev_port_attr_set(dev, &attr);
  476. if (err && err != -EOPNOTSUPP)
  477. return err;
  478. return 0;
  479. }
  480. /* Set time interval that dynamic forwarding entries live
  481. * For pure software bridge, allow values outside the 802.1
  482. * standard specification for special cases:
  483. * 0 - entry never ages (all permanant)
  484. * 1 - entry disappears (no persistance)
  485. *
  486. * Offloaded switch entries maybe more restrictive
  487. */
  488. int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
  489. {
  490. unsigned long t = clock_t_to_jiffies(ageing_time);
  491. int err;
  492. err = __set_ageing_time(br->dev, t);
  493. if (err)
  494. return err;
  495. spin_lock_bh(&br->lock);
  496. br->bridge_ageing_time = t;
  497. br->ageing_time = t;
  498. spin_unlock_bh(&br->lock);
  499. mod_delayed_work(system_long_wq, &br->gc_work, 0);
  500. return 0;
  501. }
  502. /* called under bridge lock */
  503. void __br_set_topology_change(struct net_bridge *br, unsigned char val)
  504. {
  505. unsigned long t;
  506. int err;
  507. if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
  508. /* On topology change, set the bridge ageing time to twice the
  509. * forward delay. Otherwise, restore its default ageing time.
  510. */
  511. if (val) {
  512. t = 2 * br->forward_delay;
  513. br_debug(br, "decreasing ageing time to %lu\n", t);
  514. } else {
  515. t = br->bridge_ageing_time;
  516. br_debug(br, "restoring ageing time to %lu\n", t);
  517. }
  518. err = __set_ageing_time(br->dev, t);
  519. if (err)
  520. br_warn(br, "error offloading ageing time\n");
  521. else
  522. br->ageing_time = t;
  523. }
  524. br->topology_change = val;
  525. }
  526. void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
  527. {
  528. br->bridge_forward_delay = t;
  529. if (br_is_root_bridge(br))
  530. br->forward_delay = br->bridge_forward_delay;
  531. }
  532. int br_set_forward_delay(struct net_bridge *br, unsigned long val)
  533. {
  534. unsigned long t = clock_t_to_jiffies(val);
  535. int err = -ERANGE;
  536. spin_lock_bh(&br->lock);
  537. if (br->stp_enabled != BR_NO_STP &&
  538. (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
  539. goto unlock;
  540. __br_set_forward_delay(br, t);
  541. err = 0;
  542. unlock:
  543. spin_unlock_bh(&br->lock);
  544. return err;
  545. }