blob: 21c6781906aa78d42e6aa3959e87d081e2247092 [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
Horatiu Vultur419dba8a2020-04-26 15:22:08 +020039 /* Don't change the state of the ports if they are driven by a different
40 * protocol.
41 */
42 if (p->flags & BR_MRP_AWARE)
43 return;
44
Florian Fainelli775dd692014-09-30 16:13:19 -070045 p->state = state;
Vladimir Olteandcbdf132021-02-13 22:43:17 +020046 err = switchdev_port_attr_set(p->dev, &attr, NULL);
Ido Schimmelbbe14f52015-11-13 13:06:12 +020047 if (err && err != -EOPNOTSUPP)
Scott Feldman38dcf352014-11-28 14:34:20 +010048 br_warn(p->br, "error setting offload STP state on port %u(%s)\n",
49 (unsigned int) p->port_no, p->dev->name);
Vivien Didelot7c25b162016-02-16 10:09:51 -050050 else
51 br_info(p->br, "port %u(%s) entered %s state\n",
52 (unsigned int) p->port_no, p->dev->name,
53 br_port_state_names[p->state]);
Vivien Didelotde179962019-12-11 20:07:10 -050054
55 if (p->br->stp_enabled == BR_KERNEL_STP) {
56 switch (p->state) {
57 case BR_STATE_BLOCKING:
58 p->stp_xstats.transition_blk++;
59 break;
60 case BR_STATE_FORWARDING:
61 p->stp_xstats.transition_fwd++;
62 break;
63 }
64 }
Florian Fainelli775dd692014-09-30 16:13:19 -070065}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* called under bridge lock */
68struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
69{
70 struct net_bridge_port *p;
71
Madhuparna Bhowmik33c4acb2020-02-19 20:47:46 +053072 list_for_each_entry_rcu(p, &br->port_list, list,
73 lockdep_is_held(&br->lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (p->port_no == port_no)
75 return p;
76 }
77
78 return NULL;
79}
80
81/* called under bridge lock */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090082static int br_should_become_root_port(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 u16 root_port)
84{
85 struct net_bridge *br;
86 struct net_bridge_port *rp;
87 int t;
88
89 br = p->br;
90 if (p->state == BR_STATE_DISABLED ||
91 br_is_designated_port(p))
92 return 0;
93
94 if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
95 return 0;
96
97 if (!root_port)
98 return 1;
99
100 rp = br_get_port(br, root_port);
101
102 t = memcmp(&p->designated_root, &rp->designated_root, 8);
103 if (t < 0)
104 return 1;
105 else if (t > 0)
106 return 0;
107
108 if (p->designated_cost + p->path_cost <
109 rp->designated_cost + rp->path_cost)
110 return 1;
111 else if (p->designated_cost + p->path_cost >
112 rp->designated_cost + rp->path_cost)
113 return 0;
114
115 t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
116 if (t < 0)
117 return 1;
118 else if (t > 0)
119 return 0;
120
121 if (p->designated_port < rp->designated_port)
122 return 1;
123 else if (p->designated_port > rp->designated_port)
124 return 0;
125
126 if (p->port_id < rp->port_id)
127 return 1;
128
129 return 0;
130}
131
stephen hemminger1007dd12012-11-13 07:53:08 +0000132static void br_root_port_block(const struct net_bridge *br,
133 struct net_bridge_port *p)
134{
135
136 br_notice(br, "port %u(%s) tried to become root port (blocked)",
137 (unsigned int) p->port_no, p->dev->name);
138
Florian Fainelli775dd692014-09-30 16:13:19 -0700139 br_set_state(p, BR_STATE_LISTENING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200140 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger1007dd12012-11-13 07:53:08 +0000141
142 if (br->forward_delay > 0)
143 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* called under bridge lock */
147static void br_root_selection(struct net_bridge *br)
148{
149 struct net_bridge_port *p;
150 u16 root_port = 0;
151
152 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1007dd12012-11-13 07:53:08 +0000153 if (!br_should_become_root_port(p, root_port))
154 continue;
155
156 if (p->flags & BR_ROOT_BLOCK)
157 br_root_port_block(br, p);
158 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 root_port = p->port_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 br->root_port = root_port;
163
164 if (!root_port) {
165 br->designated_root = br->bridge_id;
166 br->root_path_cost = 0;
167 } else {
168 p = br_get_port(br, root_port);
169 br->designated_root = p->designated_root;
170 br->root_path_cost = p->designated_cost + p->path_cost;
171 }
172}
173
174/* called under bridge lock */
175void br_become_root_bridge(struct net_bridge *br)
176{
177 br->max_age = br->bridge_max_age;
178 br->hello_time = br->bridge_hello_time;
179 br->forward_delay = br->bridge_forward_delay;
180 br_topology_change_detection(br);
181 del_timer(&br->tcn_timer);
182
183 if (br->dev->flags & IFF_UP) {
184 br_config_bpdu_generation(br);
185 mod_timer(&br->hello_timer, jiffies + br->hello_time);
186 }
187}
188
189/* called under bridge lock */
190void br_transmit_config(struct net_bridge_port *p)
191{
192 struct br_config_bpdu bpdu;
193 struct net_bridge *br;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (timer_pending(&p->hold_timer)) {
196 p->config_pending = 1;
197 return;
198 }
199
200 br = p->br;
201
202 bpdu.topology_change = br->topology_change;
203 bpdu.topology_change_ack = p->topology_change_ack;
204 bpdu.root = br->designated_root;
205 bpdu.root_path_cost = br->root_path_cost;
206 bpdu.bridge_id = br->bridge_id;
207 bpdu.port_id = p->port_id;
208 if (br_is_root_bridge(br))
209 bpdu.message_age = 0;
210 else {
211 struct net_bridge_port *root
212 = br_get_port(br, br->root_port);
stephen hemminger0c031502011-07-22 07:47:06 +0000213 bpdu.message_age = (jiffies - root->designated_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 + MESSAGE_AGE_INCR;
215 }
216 bpdu.max_age = br->max_age;
217 bpdu.hello_time = br->hello_time;
218 bpdu.forward_delay = br->forward_delay;
219
220 if (bpdu.message_age < br->max_age) {
221 br_send_config_bpdu(p, &bpdu);
222 p->topology_change_ack = 0;
223 p->config_pending = 0;
Nikolay Aleksandrov76b91c32015-07-23 11:01:05 -0700224 if (p->br->stp_enabled == BR_KERNEL_STP)
225 mod_timer(&p->hold_timer,
226 round_jiffies(jiffies + BR_HOLD_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228}
229
230/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000231static void br_record_config_information(struct net_bridge_port *p,
232 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 p->designated_root = bpdu->root;
235 p->designated_cost = bpdu->root_path_cost;
236 p->designated_bridge = bpdu->bridge_id;
237 p->designated_port = bpdu->port_id;
Joakim Tjernlund709e1b52012-03-01 08:12:19 +0000238 p->designated_age = jiffies - bpdu->message_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900240 mod_timer(&p->message_age_timer, jiffies
Chris Healy9a062012013-09-11 21:37:47 -0700241 + (bpdu->max_age - bpdu->message_age));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000245static void br_record_config_timeout_values(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 const struct br_config_bpdu *bpdu)
247{
248 br->max_age = bpdu->max_age;
249 br->hello_time = bpdu->hello_time;
250 br->forward_delay = bpdu->forward_delay;
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500251 __br_set_topology_change(br, bpdu->topology_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/* called under bridge lock */
255void br_transmit_tcn(struct net_bridge *br)
256{
stephen hemminger91bc0332013-04-30 05:29:27 +0000257 struct net_bridge_port *p;
258
259 p = br_get_port(br, br->root_port);
260 if (p)
261 br_send_tcn_bpdu(p);
262 else
263 br_notice(br, "root port %u not found for topology notice\n",
264 br->root_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/* called under bridge lock */
268static int br_should_become_designated_port(const struct net_bridge_port *p)
269{
270 struct net_bridge *br;
271 int t;
272
273 br = p->br;
274 if (br_is_designated_port(p))
275 return 1;
276
277 if (memcmp(&p->designated_root, &br->designated_root, 8))
278 return 1;
279
280 if (br->root_path_cost < p->designated_cost)
281 return 1;
282 else if (br->root_path_cost > p->designated_cost)
283 return 0;
284
285 t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
286 if (t < 0)
287 return 1;
288 else if (t > 0)
289 return 0;
290
291 if (p->port_id < p->designated_port)
292 return 1;
293
294 return 0;
295}
296
297/* called under bridge lock */
298static void br_designated_port_selection(struct net_bridge *br)
299{
300 struct net_bridge_port *p;
301
302 list_for_each_entry(p, &br->port_list, list) {
303 if (p->state != BR_STATE_DISABLED &&
304 br_should_become_designated_port(p))
305 br_become_designated_port(p);
306
307 }
308}
309
310/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000311static int br_supersedes_port_info(const struct net_bridge_port *p,
312 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 int t;
315
316 t = memcmp(&bpdu->root, &p->designated_root, 8);
317 if (t < 0)
318 return 1;
319 else if (t > 0)
320 return 0;
321
322 if (bpdu->root_path_cost < p->designated_cost)
323 return 1;
324 else if (bpdu->root_path_cost > p->designated_cost)
325 return 0;
326
327 t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
328 if (t < 0)
329 return 1;
330 else if (t > 0)
331 return 0;
332
333 if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
334 return 1;
335
336 if (bpdu->port_id <= p->designated_port)
337 return 1;
338
339 return 0;
340}
341
342/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000343static void br_topology_change_acknowledged(struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 br->topology_change_detected = 0;
346 del_timer(&br->tcn_timer);
347}
348
349/* called under bridge lock */
350void br_topology_change_detection(struct net_bridge *br)
351{
352 int isroot = br_is_root_bridge(br);
353
Stephen Hemminger4f0611a2009-05-15 06:11:58 +0000354 if (br->stp_enabled != BR_KERNEL_STP)
355 return;
356
stephen hemminger28a16c92010-05-10 09:31:09 +0000357 br_info(br, "topology change detected, %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 isroot ? "propagating" : "sending tcn bpdu");
359
360 if (isroot) {
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500361 __br_set_topology_change(br, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 mod_timer(&br->topology_change_timer, jiffies
363 + br->bridge_forward_delay + br->bridge_max_age);
364 } else if (!br->topology_change_detected) {
365 br_transmit_tcn(br);
366 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
367 }
368
369 br->topology_change_detected = 1;
370}
371
372/* called under bridge lock */
373void br_config_bpdu_generation(struct net_bridge *br)
374{
375 struct net_bridge_port *p;
376
377 list_for_each_entry(p, &br->port_list, list) {
378 if (p->state != BR_STATE_DISABLED &&
379 br_is_designated_port(p))
380 br_transmit_config(p);
381 }
382}
383
384/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000385static void br_reply(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 br_transmit_config(p);
388}
389
390/* called under bridge lock */
391void br_configuration_update(struct net_bridge *br)
392{
393 br_root_selection(br);
394 br_designated_port_selection(br);
395}
396
397/* called under bridge lock */
398void br_become_designated_port(struct net_bridge_port *p)
399{
400 struct net_bridge *br;
401
402 br = p->br;
403 p->designated_root = br->designated_root;
404 p->designated_cost = br->root_path_cost;
405 p->designated_bridge = br->bridge_id;
406 p->designated_port = p->port_id;
407}
408
409
410/* called under bridge lock */
411static void br_make_blocking(struct net_bridge_port *p)
412{
413 if (p->state != BR_STATE_DISABLED &&
414 p->state != BR_STATE_BLOCKING) {
415 if (p->state == BR_STATE_FORWARDING ||
416 p->state == BR_STATE_LEARNING)
417 br_topology_change_detection(p->br);
418
Florian Fainelli775dd692014-09-30 16:13:19 -0700419 br_set_state(p, BR_STATE_BLOCKING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200420 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger4ecb9612011-07-22 07:47:09 +0000421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 del_timer(&p->forward_delay_timer);
423 }
424}
425
426/* called under bridge lock */
427static void br_make_forwarding(struct net_bridge_port *p)
428{
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700429 struct net_bridge *br = p->br;
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700430
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700431 if (p->state != BR_STATE_BLOCKING)
432 return;
433
stephen hemmingera461c022011-03-10 05:57:04 +0000434 if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
Florian Fainelli775dd692014-09-30 16:13:19 -0700435 br_set_state(p, BR_STATE_FORWARDING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700436 br_topology_change_detection(br);
437 del_timer(&p->forward_delay_timer);
stephen hemminger160d73b2011-07-22 07:47:10 +0000438 } else if (br->stp_enabled == BR_KERNEL_STP)
Florian Fainelli775dd692014-09-30 16:13:19 -0700439 br_set_state(p, BR_STATE_LISTENING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700440 else
Florian Fainelli775dd692014-09-30 16:13:19 -0700441 br_set_state(p, BR_STATE_LEARNING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700442
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200443 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700444
445 if (br->forward_delay != 0)
446 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/* called under bridge lock */
450void br_port_state_selection(struct net_bridge *br)
451{
452 struct net_bridge_port *p;
stephen hemminger1faa4352011-03-07 08:34:06 +0000453 unsigned int liveports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1faa4352011-03-07 08:34:06 +0000456 if (p->state == BR_STATE_DISABLED)
457 continue;
458
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000459 /* Don't change port states if userspace is handling STP */
460 if (br->stp_enabled != BR_USER_STP) {
461 if (p->port_no == br->root_port) {
462 p->config_pending = 0;
463 p->topology_change_ack = 0;
464 br_make_forwarding(p);
465 } else if (br_is_designated_port(p)) {
466 del_timer(&p->message_age_timer);
467 br_make_forwarding(p);
468 } else {
469 p->config_pending = 0;
470 p->topology_change_ack = 0;
471 br_make_blocking(p);
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Nikolay Aleksandrov7ce42de2015-06-19 01:45:50 -0700475 if (p->state != BR_STATE_BLOCKING)
476 br_multicast_enable_port(p);
Nikolay Aleksandrov9aa66382015-06-23 04:47:44 -0700477 /* Multicast is not disabled for the port when it goes in
478 * blocking state because the timers will expire and stop by
479 * themselves without sending more queries.
480 */
stephen hemminger1faa4352011-03-07 08:34:06 +0000481 if (p->state == BR_STATE_FORWARDING)
482 ++liveports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
stephen hemminger1faa4352011-03-07 08:34:06 +0000484
485 if (liveports == 0)
486 netif_carrier_off(br->dev);
487 else
488 netif_carrier_on(br->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000492static void br_topology_change_acknowledge(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 p->topology_change_ack = 1;
495 br_transmit_config(p);
496}
497
498/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000499void br_received_config_bpdu(struct net_bridge_port *p,
500 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct net_bridge *br;
503 int was_root;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900504
Vivien Didelotde179962019-12-11 20:07:10 -0500505 p->stp_xstats.rx_bpdu++;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 br = p->br;
508 was_root = br_is_root_bridge(br);
509
510 if (br_supersedes_port_info(p, bpdu)) {
511 br_record_config_information(p, bpdu);
512 br_configuration_update(br);
513 br_port_state_selection(br);
514
515 if (!br_is_root_bridge(br) && was_root) {
516 del_timer(&br->hello_timer);
517 if (br->topology_change_detected) {
518 del_timer(&br->topology_change_timer);
519 br_transmit_tcn(br);
520
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900521 mod_timer(&br->tcn_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 jiffies + br->bridge_hello_time);
523 }
524 }
525
526 if (p->port_no == br->root_port) {
527 br_record_config_timeout_values(br, bpdu);
528 br_config_bpdu_generation(br);
529 if (bpdu->topology_change_ack)
530 br_topology_change_acknowledged(br);
531 }
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900532 } else if (br_is_designated_port(p)) {
533 br_reply(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535}
536
537/* called under bridge lock */
538void br_received_tcn_bpdu(struct net_bridge_port *p)
539{
Vivien Didelotde179962019-12-11 20:07:10 -0500540 p->stp_xstats.rx_tcn++;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (br_is_designated_port(p)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000543 br_info(p->br, "port %u(%s) received tcn bpdu\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000544 (unsigned int) p->port_no, p->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 br_topology_change_detection(p->br);
547 br_topology_change_acknowledge(p);
548 }
549}
stephen hemminger14f98f22011-04-04 14:03:33 +0000550
551/* Change bridge STP parameter */
552int br_set_hello_time(struct net_bridge *br, unsigned long val)
553{
554 unsigned long t = clock_t_to_jiffies(val);
555
556 if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
557 return -ERANGE;
558
559 spin_lock_bh(&br->lock);
560 br->bridge_hello_time = t;
561 if (br_is_root_bridge(br))
562 br->hello_time = br->bridge_hello_time;
563 spin_unlock_bh(&br->lock);
564 return 0;
565}
566
567int br_set_max_age(struct net_bridge *br, unsigned long val)
568{
569 unsigned long t = clock_t_to_jiffies(val);
570
571 if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
572 return -ERANGE;
573
574 spin_lock_bh(&br->lock);
575 br->bridge_max_age = t;
576 if (br_is_root_bridge(br))
577 br->max_age = br->bridge_max_age;
578 spin_unlock_bh(&br->lock);
579 return 0;
580
581}
582
Vivien Didelot82dd4332016-12-10 13:44:27 -0500583/* called under bridge lock */
584int __set_ageing_time(struct net_device *dev, unsigned long t)
585{
586 struct switchdev_attr attr = {
587 .orig_dev = dev,
588 .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
589 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
590 .u.ageing_time = jiffies_to_clock_t(t),
591 };
592 int err;
593
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200594 err = switchdev_port_attr_set(dev, &attr, NULL);
Vivien Didelot82dd4332016-12-10 13:44:27 -0500595 if (err && err != -EOPNOTSUPP)
596 return err;
597
598 return 0;
599}
600
Stephen Hemminger4c656c12016-03-08 12:59:35 -0800601/* Set time interval that dynamic forwarding entries live
602 * For pure software bridge, allow values outside the 802.1
603 * standard specification for special cases:
Menglong Dongefb5b332021-01-07 18:53:32 -0800604 * 0 - entry never ages (all permanent)
605 * 1 - entry disappears (no persistence)
Stephen Hemminger4c656c12016-03-08 12:59:35 -0800606 *
607 * Offloaded switch entries maybe more restrictive
608 */
Vivien Didelot9e0b27f2016-07-21 12:42:19 -0400609int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700610{
Scott Feldmanc62987b2015-10-08 19:23:19 -0700611 unsigned long t = clock_t_to_jiffies(ageing_time);
612 int err;
613
Vivien Didelot82dd4332016-12-10 13:44:27 -0500614 err = __set_ageing_time(br->dev, t);
615 if (err)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700616 return err;
617
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500618 spin_lock_bh(&br->lock);
619 br->bridge_ageing_time = t;
Scott Feldmanc62987b2015-10-08 19:23:19 -0700620 br->ageing_time = t;
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500621 spin_unlock_bh(&br->lock);
622
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100623 mod_delayed_work(system_long_wq, &br->gc_work, 0);
Scott Feldmanc62987b2015-10-08 19:23:19 -0700624
625 return 0;
626}
627
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500628/* called under bridge lock */
629void __br_set_topology_change(struct net_bridge *br, unsigned char val)
630{
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500631 unsigned long t;
632 int err;
633
634 if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
635 /* On topology change, set the bridge ageing time to twice the
636 * forward delay. Otherwise, restore its default ageing time.
637 */
638
639 if (val) {
640 t = 2 * br->forward_delay;
641 br_debug(br, "decreasing ageing time to %lu\n", t);
642 } else {
643 t = br->bridge_ageing_time;
644 br_debug(br, "restoring ageing time to %lu\n", t);
645 }
646
647 err = __set_ageing_time(br->dev, t);
648 if (err)
649 br_warn(br, "error offloading ageing time\n");
650 else
651 br->ageing_time = t;
652 }
653
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500654 br->topology_change = val;
655}
656
Herbert Xube4f1542013-09-12 17:12:05 +1000657void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
stephen hemminger14f98f22011-04-04 14:03:33 +0000658{
stephen hemminger14f98f22011-04-04 14:03:33 +0000659 br->bridge_forward_delay = t;
660 if (br_is_root_bridge(br))
661 br->forward_delay = br->bridge_forward_delay;
Herbert Xube4f1542013-09-12 17:12:05 +1000662}
663
664int br_set_forward_delay(struct net_bridge *br, unsigned long val)
665{
666 unsigned long t = clock_t_to_jiffies(val);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500667 int err = -ERANGE;
Herbert Xube4f1542013-09-12 17:12:05 +1000668
669 spin_lock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500670 if (br->stp_enabled != BR_NO_STP &&
671 (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
672 goto unlock;
673
Herbert Xube4f1542013-09-12 17:12:05 +1000674 __br_set_forward_delay(br, t);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500675 err = 0;
676
677unlock:
stephen hemminger14f98f22011-04-04 14:03:33 +0000678 spin_unlock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500679 return err;
stephen hemminger14f98f22011-04-04 14:03:33 +0000680}