blob: 6856a6d9282b830fce00cce256d671c17c08638f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Spanning tree protocol; generic parts
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/kernel.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020010#include <linux/rculist.h>
Scott Feldman38dcf352014-11-28 14:34:20 +010011#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "br_private.h"
14#include "br_private_stp.h"
15
16/* since time values in bpdu are in jiffies and then scaled (1/256)
Joakim Tjernlundaaca7352012-03-01 08:12:18 +000017 * before sending, make sure that is at least one STP tick.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joakim Tjernlundaaca7352012-03-01 08:12:18 +000019#define MESSAGE_AGE_INCR ((HZ / 256) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070021static const char *const br_port_state_names[] = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090022 [BR_STATE_DISABLED] = "disabled",
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 [BR_STATE_LISTENING] = "listening",
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090024 [BR_STATE_LEARNING] = "learning",
25 [BR_STATE_FORWARDING] = "forwarding",
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 [BR_STATE_BLOCKING] = "blocking",
27};
28
Florian Fainelli775dd692014-09-30 16:13:19 -070029void br_set_state(struct net_bridge_port *p, unsigned int state)
30{
Scott Feldman35636062015-05-10 09:47:51 -070031 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010032 .orig_dev = p->dev,
Jiri Pirko1f868392015-10-01 11:03:42 +020033 .id = SWITCHDEV_ATTR_ID_PORT_STP_STATE,
Jiri Pirko0bc05d52015-10-14 19:40:50 +020034 .flags = SWITCHDEV_F_DEFER,
Scott Feldman42275bd2015-05-13 11:16:50 -070035 .u.stp_state = state,
Scott Feldman35636062015-05-10 09:47:51 -070036 };
Scott Feldman38dcf352014-11-28 14:34:20 +010037 int err;
38
Florian Fainelli775dd692014-09-30 16:13:19 -070039 p->state = state;
Scott Feldman35636062015-05-10 09:47:51 -070040 err = switchdev_port_attr_set(p->dev, &attr);
Ido Schimmelbbe14f52015-11-13 13:06:12 +020041 if (err && err != -EOPNOTSUPP)
Scott Feldman38dcf352014-11-28 14:34:20 +010042 br_warn(p->br, "error setting offload STP state on port %u(%s)\n",
43 (unsigned int) p->port_no, p->dev->name);
Vivien Didelot7c25b162016-02-16 10:09:51 -050044 else
45 br_info(p->br, "port %u(%s) entered %s state\n",
46 (unsigned int) p->port_no, p->dev->name,
47 br_port_state_names[p->state]);
Vivien Didelotde179962019-12-11 20:07:10 -050048
49 if (p->br->stp_enabled == BR_KERNEL_STP) {
50 switch (p->state) {
51 case BR_STATE_BLOCKING:
52 p->stp_xstats.transition_blk++;
53 break;
54 case BR_STATE_FORWARDING:
55 p->stp_xstats.transition_fwd++;
56 break;
57 }
58 }
Florian Fainelli775dd692014-09-30 16:13:19 -070059}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* called under bridge lock */
62struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
63{
64 struct net_bridge_port *p;
65
66 list_for_each_entry_rcu(p, &br->port_list, list) {
67 if (p->port_no == port_no)
68 return p;
69 }
70
71 return NULL;
72}
73
74/* called under bridge lock */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090075static int br_should_become_root_port(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u16 root_port)
77{
78 struct net_bridge *br;
79 struct net_bridge_port *rp;
80 int t;
81
82 br = p->br;
83 if (p->state == BR_STATE_DISABLED ||
84 br_is_designated_port(p))
85 return 0;
86
87 if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
88 return 0;
89
90 if (!root_port)
91 return 1;
92
93 rp = br_get_port(br, root_port);
94
95 t = memcmp(&p->designated_root, &rp->designated_root, 8);
96 if (t < 0)
97 return 1;
98 else if (t > 0)
99 return 0;
100
101 if (p->designated_cost + p->path_cost <
102 rp->designated_cost + rp->path_cost)
103 return 1;
104 else if (p->designated_cost + p->path_cost >
105 rp->designated_cost + rp->path_cost)
106 return 0;
107
108 t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
109 if (t < 0)
110 return 1;
111 else if (t > 0)
112 return 0;
113
114 if (p->designated_port < rp->designated_port)
115 return 1;
116 else if (p->designated_port > rp->designated_port)
117 return 0;
118
119 if (p->port_id < rp->port_id)
120 return 1;
121
122 return 0;
123}
124
stephen hemminger1007dd12012-11-13 07:53:08 +0000125static void br_root_port_block(const struct net_bridge *br,
126 struct net_bridge_port *p)
127{
128
129 br_notice(br, "port %u(%s) tried to become root port (blocked)",
130 (unsigned int) p->port_no, p->dev->name);
131
Florian Fainelli775dd692014-09-30 16:13:19 -0700132 br_set_state(p, BR_STATE_LISTENING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200133 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger1007dd12012-11-13 07:53:08 +0000134
135 if (br->forward_delay > 0)
136 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* called under bridge lock */
140static void br_root_selection(struct net_bridge *br)
141{
142 struct net_bridge_port *p;
143 u16 root_port = 0;
144
145 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1007dd12012-11-13 07:53:08 +0000146 if (!br_should_become_root_port(p, root_port))
147 continue;
148
149 if (p->flags & BR_ROOT_BLOCK)
150 br_root_port_block(br, p);
151 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 root_port = p->port_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 br->root_port = root_port;
156
157 if (!root_port) {
158 br->designated_root = br->bridge_id;
159 br->root_path_cost = 0;
160 } else {
161 p = br_get_port(br, root_port);
162 br->designated_root = p->designated_root;
163 br->root_path_cost = p->designated_cost + p->path_cost;
164 }
165}
166
167/* called under bridge lock */
168void br_become_root_bridge(struct net_bridge *br)
169{
170 br->max_age = br->bridge_max_age;
171 br->hello_time = br->bridge_hello_time;
172 br->forward_delay = br->bridge_forward_delay;
173 br_topology_change_detection(br);
174 del_timer(&br->tcn_timer);
175
176 if (br->dev->flags & IFF_UP) {
177 br_config_bpdu_generation(br);
178 mod_timer(&br->hello_timer, jiffies + br->hello_time);
179 }
180}
181
182/* called under bridge lock */
183void br_transmit_config(struct net_bridge_port *p)
184{
185 struct br_config_bpdu bpdu;
186 struct net_bridge *br;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (timer_pending(&p->hold_timer)) {
189 p->config_pending = 1;
190 return;
191 }
192
193 br = p->br;
194
195 bpdu.topology_change = br->topology_change;
196 bpdu.topology_change_ack = p->topology_change_ack;
197 bpdu.root = br->designated_root;
198 bpdu.root_path_cost = br->root_path_cost;
199 bpdu.bridge_id = br->bridge_id;
200 bpdu.port_id = p->port_id;
201 if (br_is_root_bridge(br))
202 bpdu.message_age = 0;
203 else {
204 struct net_bridge_port *root
205 = br_get_port(br, br->root_port);
stephen hemminger0c031502011-07-22 07:47:06 +0000206 bpdu.message_age = (jiffies - root->designated_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 + MESSAGE_AGE_INCR;
208 }
209 bpdu.max_age = br->max_age;
210 bpdu.hello_time = br->hello_time;
211 bpdu.forward_delay = br->forward_delay;
212
213 if (bpdu.message_age < br->max_age) {
214 br_send_config_bpdu(p, &bpdu);
215 p->topology_change_ack = 0;
216 p->config_pending = 0;
Nikolay Aleksandrov76b91c32015-07-23 11:01:05 -0700217 if (p->br->stp_enabled == BR_KERNEL_STP)
218 mod_timer(&p->hold_timer,
219 round_jiffies(jiffies + BR_HOLD_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221}
222
223/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000224static void br_record_config_information(struct net_bridge_port *p,
225 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 p->designated_root = bpdu->root;
228 p->designated_cost = bpdu->root_path_cost;
229 p->designated_bridge = bpdu->bridge_id;
230 p->designated_port = bpdu->port_id;
Joakim Tjernlund709e1b52012-03-01 08:12:19 +0000231 p->designated_age = jiffies - bpdu->message_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900233 mod_timer(&p->message_age_timer, jiffies
Chris Healy9a062012013-09-11 21:37:47 -0700234 + (bpdu->max_age - bpdu->message_age));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000238static void br_record_config_timeout_values(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 const struct br_config_bpdu *bpdu)
240{
241 br->max_age = bpdu->max_age;
242 br->hello_time = bpdu->hello_time;
243 br->forward_delay = bpdu->forward_delay;
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500244 __br_set_topology_change(br, bpdu->topology_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247/* called under bridge lock */
248void br_transmit_tcn(struct net_bridge *br)
249{
stephen hemminger91bc0332013-04-30 05:29:27 +0000250 struct net_bridge_port *p;
251
252 p = br_get_port(br, br->root_port);
253 if (p)
254 br_send_tcn_bpdu(p);
255 else
256 br_notice(br, "root port %u not found for topology notice\n",
257 br->root_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/* called under bridge lock */
261static int br_should_become_designated_port(const struct net_bridge_port *p)
262{
263 struct net_bridge *br;
264 int t;
265
266 br = p->br;
267 if (br_is_designated_port(p))
268 return 1;
269
270 if (memcmp(&p->designated_root, &br->designated_root, 8))
271 return 1;
272
273 if (br->root_path_cost < p->designated_cost)
274 return 1;
275 else if (br->root_path_cost > p->designated_cost)
276 return 0;
277
278 t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
279 if (t < 0)
280 return 1;
281 else if (t > 0)
282 return 0;
283
284 if (p->port_id < p->designated_port)
285 return 1;
286
287 return 0;
288}
289
290/* called under bridge lock */
291static void br_designated_port_selection(struct net_bridge *br)
292{
293 struct net_bridge_port *p;
294
295 list_for_each_entry(p, &br->port_list, list) {
296 if (p->state != BR_STATE_DISABLED &&
297 br_should_become_designated_port(p))
298 br_become_designated_port(p);
299
300 }
301}
302
303/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000304static int br_supersedes_port_info(const struct net_bridge_port *p,
305 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int t;
308
309 t = memcmp(&bpdu->root, &p->designated_root, 8);
310 if (t < 0)
311 return 1;
312 else if (t > 0)
313 return 0;
314
315 if (bpdu->root_path_cost < p->designated_cost)
316 return 1;
317 else if (bpdu->root_path_cost > p->designated_cost)
318 return 0;
319
320 t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
321 if (t < 0)
322 return 1;
323 else if (t > 0)
324 return 0;
325
326 if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
327 return 1;
328
329 if (bpdu->port_id <= p->designated_port)
330 return 1;
331
332 return 0;
333}
334
335/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000336static void br_topology_change_acknowledged(struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 br->topology_change_detected = 0;
339 del_timer(&br->tcn_timer);
340}
341
342/* called under bridge lock */
343void br_topology_change_detection(struct net_bridge *br)
344{
345 int isroot = br_is_root_bridge(br);
346
Stephen Hemminger4f0611a2009-05-15 06:11:58 +0000347 if (br->stp_enabled != BR_KERNEL_STP)
348 return;
349
stephen hemminger28a16c92010-05-10 09:31:09 +0000350 br_info(br, "topology change detected, %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 isroot ? "propagating" : "sending tcn bpdu");
352
353 if (isroot) {
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500354 __br_set_topology_change(br, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 mod_timer(&br->topology_change_timer, jiffies
356 + br->bridge_forward_delay + br->bridge_max_age);
357 } else if (!br->topology_change_detected) {
358 br_transmit_tcn(br);
359 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
360 }
361
362 br->topology_change_detected = 1;
363}
364
365/* called under bridge lock */
366void br_config_bpdu_generation(struct net_bridge *br)
367{
368 struct net_bridge_port *p;
369
370 list_for_each_entry(p, &br->port_list, list) {
371 if (p->state != BR_STATE_DISABLED &&
372 br_is_designated_port(p))
373 br_transmit_config(p);
374 }
375}
376
377/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000378static void br_reply(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 br_transmit_config(p);
381}
382
383/* called under bridge lock */
384void br_configuration_update(struct net_bridge *br)
385{
386 br_root_selection(br);
387 br_designated_port_selection(br);
388}
389
390/* called under bridge lock */
391void br_become_designated_port(struct net_bridge_port *p)
392{
393 struct net_bridge *br;
394
395 br = p->br;
396 p->designated_root = br->designated_root;
397 p->designated_cost = br->root_path_cost;
398 p->designated_bridge = br->bridge_id;
399 p->designated_port = p->port_id;
400}
401
402
403/* called under bridge lock */
404static void br_make_blocking(struct net_bridge_port *p)
405{
406 if (p->state != BR_STATE_DISABLED &&
407 p->state != BR_STATE_BLOCKING) {
408 if (p->state == BR_STATE_FORWARDING ||
409 p->state == BR_STATE_LEARNING)
410 br_topology_change_detection(p->br);
411
Florian Fainelli775dd692014-09-30 16:13:19 -0700412 br_set_state(p, BR_STATE_BLOCKING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200413 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger4ecb9612011-07-22 07:47:09 +0000414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 del_timer(&p->forward_delay_timer);
416 }
417}
418
419/* called under bridge lock */
420static void br_make_forwarding(struct net_bridge_port *p)
421{
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700422 struct net_bridge *br = p->br;
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700423
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700424 if (p->state != BR_STATE_BLOCKING)
425 return;
426
stephen hemmingera461c022011-03-10 05:57:04 +0000427 if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
Florian Fainelli775dd692014-09-30 16:13:19 -0700428 br_set_state(p, BR_STATE_FORWARDING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700429 br_topology_change_detection(br);
430 del_timer(&p->forward_delay_timer);
stephen hemminger160d73b2011-07-22 07:47:10 +0000431 } else if (br->stp_enabled == BR_KERNEL_STP)
Florian Fainelli775dd692014-09-30 16:13:19 -0700432 br_set_state(p, BR_STATE_LISTENING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700433 else
Florian Fainelli775dd692014-09-30 16:13:19 -0700434 br_set_state(p, BR_STATE_LEARNING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700435
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200436 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700437
438 if (br->forward_delay != 0)
439 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/* called under bridge lock */
443void br_port_state_selection(struct net_bridge *br)
444{
445 struct net_bridge_port *p;
stephen hemminger1faa4352011-03-07 08:34:06 +0000446 unsigned int liveports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1faa4352011-03-07 08:34:06 +0000449 if (p->state == BR_STATE_DISABLED)
450 continue;
451
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000452 /* Don't change port states if userspace is handling STP */
453 if (br->stp_enabled != BR_USER_STP) {
454 if (p->port_no == br->root_port) {
455 p->config_pending = 0;
456 p->topology_change_ack = 0;
457 br_make_forwarding(p);
458 } else if (br_is_designated_port(p)) {
459 del_timer(&p->message_age_timer);
460 br_make_forwarding(p);
461 } else {
462 p->config_pending = 0;
463 p->topology_change_ack = 0;
464 br_make_blocking(p);
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
Nikolay Aleksandrov7ce42de2015-06-19 01:45:50 -0700468 if (p->state != BR_STATE_BLOCKING)
469 br_multicast_enable_port(p);
Nikolay Aleksandrov9aa66382015-06-23 04:47:44 -0700470 /* Multicast is not disabled for the port when it goes in
471 * blocking state because the timers will expire and stop by
472 * themselves without sending more queries.
473 */
stephen hemminger1faa4352011-03-07 08:34:06 +0000474 if (p->state == BR_STATE_FORWARDING)
475 ++liveports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
stephen hemminger1faa4352011-03-07 08:34:06 +0000477
478 if (liveports == 0)
479 netif_carrier_off(br->dev);
480 else
481 netif_carrier_on(br->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000485static void br_topology_change_acknowledge(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 p->topology_change_ack = 1;
488 br_transmit_config(p);
489}
490
491/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000492void br_received_config_bpdu(struct net_bridge_port *p,
493 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct net_bridge *br;
496 int was_root;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900497
Vivien Didelotde179962019-12-11 20:07:10 -0500498 p->stp_xstats.rx_bpdu++;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 br = p->br;
501 was_root = br_is_root_bridge(br);
502
503 if (br_supersedes_port_info(p, bpdu)) {
504 br_record_config_information(p, bpdu);
505 br_configuration_update(br);
506 br_port_state_selection(br);
507
508 if (!br_is_root_bridge(br) && was_root) {
509 del_timer(&br->hello_timer);
510 if (br->topology_change_detected) {
511 del_timer(&br->topology_change_timer);
512 br_transmit_tcn(br);
513
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900514 mod_timer(&br->tcn_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 jiffies + br->bridge_hello_time);
516 }
517 }
518
519 if (p->port_no == br->root_port) {
520 br_record_config_timeout_values(br, bpdu);
521 br_config_bpdu_generation(br);
522 if (bpdu->topology_change_ack)
523 br_topology_change_acknowledged(br);
524 }
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900525 } else if (br_is_designated_port(p)) {
526 br_reply(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528}
529
530/* called under bridge lock */
531void br_received_tcn_bpdu(struct net_bridge_port *p)
532{
Vivien Didelotde179962019-12-11 20:07:10 -0500533 p->stp_xstats.rx_tcn++;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (br_is_designated_port(p)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000536 br_info(p->br, "port %u(%s) received tcn bpdu\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000537 (unsigned int) p->port_no, p->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 br_topology_change_detection(p->br);
540 br_topology_change_acknowledge(p);
541 }
542}
stephen hemminger14f98f22011-04-04 14:03:33 +0000543
544/* Change bridge STP parameter */
545int br_set_hello_time(struct net_bridge *br, unsigned long val)
546{
547 unsigned long t = clock_t_to_jiffies(val);
548
549 if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
550 return -ERANGE;
551
552 spin_lock_bh(&br->lock);
553 br->bridge_hello_time = t;
554 if (br_is_root_bridge(br))
555 br->hello_time = br->bridge_hello_time;
556 spin_unlock_bh(&br->lock);
557 return 0;
558}
559
560int br_set_max_age(struct net_bridge *br, unsigned long val)
561{
562 unsigned long t = clock_t_to_jiffies(val);
563
564 if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
565 return -ERANGE;
566
567 spin_lock_bh(&br->lock);
568 br->bridge_max_age = t;
569 if (br_is_root_bridge(br))
570 br->max_age = br->bridge_max_age;
571 spin_unlock_bh(&br->lock);
572 return 0;
573
574}
575
Vivien Didelot82dd4332016-12-10 13:44:27 -0500576/* called under bridge lock */
577int __set_ageing_time(struct net_device *dev, unsigned long t)
578{
579 struct switchdev_attr attr = {
580 .orig_dev = dev,
581 .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
582 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
583 .u.ageing_time = jiffies_to_clock_t(t),
584 };
585 int err;
586
587 err = switchdev_port_attr_set(dev, &attr);
588 if (err && err != -EOPNOTSUPP)
589 return err;
590
591 return 0;
592}
593
Stephen Hemminger4c656c12016-03-08 12:59:35 -0800594/* Set time interval that dynamic forwarding entries live
595 * For pure software bridge, allow values outside the 802.1
596 * standard specification for special cases:
597 * 0 - entry never ages (all permanant)
598 * 1 - entry disappears (no persistance)
599 *
600 * Offloaded switch entries maybe more restrictive
601 */
Vivien Didelot9e0b27f2016-07-21 12:42:19 -0400602int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700603{
Scott Feldmanc62987b2015-10-08 19:23:19 -0700604 unsigned long t = clock_t_to_jiffies(ageing_time);
605 int err;
606
Vivien Didelot82dd4332016-12-10 13:44:27 -0500607 err = __set_ageing_time(br->dev, t);
608 if (err)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700609 return err;
610
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500611 spin_lock_bh(&br->lock);
612 br->bridge_ageing_time = t;
Scott Feldmanc62987b2015-10-08 19:23:19 -0700613 br->ageing_time = t;
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500614 spin_unlock_bh(&br->lock);
615
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100616 mod_delayed_work(system_long_wq, &br->gc_work, 0);
Scott Feldmanc62987b2015-10-08 19:23:19 -0700617
618 return 0;
619}
620
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500621/* called under bridge lock */
622void __br_set_topology_change(struct net_bridge *br, unsigned char val)
623{
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500624 unsigned long t;
625 int err;
626
627 if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
628 /* On topology change, set the bridge ageing time to twice the
629 * forward delay. Otherwise, restore its default ageing time.
630 */
631
632 if (val) {
633 t = 2 * br->forward_delay;
634 br_debug(br, "decreasing ageing time to %lu\n", t);
635 } else {
636 t = br->bridge_ageing_time;
637 br_debug(br, "restoring ageing time to %lu\n", t);
638 }
639
640 err = __set_ageing_time(br->dev, t);
641 if (err)
642 br_warn(br, "error offloading ageing time\n");
643 else
644 br->ageing_time = t;
645 }
646
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500647 br->topology_change = val;
648}
649
Herbert Xube4f1542013-09-12 17:12:05 +1000650void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
stephen hemminger14f98f22011-04-04 14:03:33 +0000651{
stephen hemminger14f98f22011-04-04 14:03:33 +0000652 br->bridge_forward_delay = t;
653 if (br_is_root_bridge(br))
654 br->forward_delay = br->bridge_forward_delay;
Herbert Xube4f1542013-09-12 17:12:05 +1000655}
656
657int br_set_forward_delay(struct net_bridge *br, unsigned long val)
658{
659 unsigned long t = clock_t_to_jiffies(val);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500660 int err = -ERANGE;
Herbert Xube4f1542013-09-12 17:12:05 +1000661
662 spin_lock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500663 if (br->stp_enabled != BR_NO_STP &&
664 (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
665 goto unlock;
666
Herbert Xube4f1542013-09-12 17:12:05 +1000667 __br_set_forward_delay(br, t);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500668 err = 0;
669
670unlock:
stephen hemminger14f98f22011-04-04 14:03:33 +0000671 spin_unlock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500672 return err;
stephen hemminger14f98f22011-04-04 14:03:33 +0000673}