blob: 1f14b84553457053479d2019f2ec9d8e6e2c41b1 [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
Madhuparna Bhowmik33c4acb2020-02-19 20:47:46 +053066 list_for_each_entry_rcu(p, &br->port_list, list,
67 lockdep_is_held(&br->lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (p->port_no == port_no)
69 return p;
70 }
71
72 return NULL;
73}
74
75/* called under bridge lock */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090076static int br_should_become_root_port(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 u16 root_port)
78{
79 struct net_bridge *br;
80 struct net_bridge_port *rp;
81 int t;
82
83 br = p->br;
84 if (p->state == BR_STATE_DISABLED ||
85 br_is_designated_port(p))
86 return 0;
87
88 if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
89 return 0;
90
91 if (!root_port)
92 return 1;
93
94 rp = br_get_port(br, root_port);
95
96 t = memcmp(&p->designated_root, &rp->designated_root, 8);
97 if (t < 0)
98 return 1;
99 else if (t > 0)
100 return 0;
101
102 if (p->designated_cost + p->path_cost <
103 rp->designated_cost + rp->path_cost)
104 return 1;
105 else if (p->designated_cost + p->path_cost >
106 rp->designated_cost + rp->path_cost)
107 return 0;
108
109 t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
110 if (t < 0)
111 return 1;
112 else if (t > 0)
113 return 0;
114
115 if (p->designated_port < rp->designated_port)
116 return 1;
117 else if (p->designated_port > rp->designated_port)
118 return 0;
119
120 if (p->port_id < rp->port_id)
121 return 1;
122
123 return 0;
124}
125
stephen hemminger1007dd12012-11-13 07:53:08 +0000126static void br_root_port_block(const struct net_bridge *br,
127 struct net_bridge_port *p)
128{
129
130 br_notice(br, "port %u(%s) tried to become root port (blocked)",
131 (unsigned int) p->port_no, p->dev->name);
132
Florian Fainelli775dd692014-09-30 16:13:19 -0700133 br_set_state(p, BR_STATE_LISTENING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200134 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger1007dd12012-11-13 07:53:08 +0000135
136 if (br->forward_delay > 0)
137 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* called under bridge lock */
141static void br_root_selection(struct net_bridge *br)
142{
143 struct net_bridge_port *p;
144 u16 root_port = 0;
145
146 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1007dd12012-11-13 07:53:08 +0000147 if (!br_should_become_root_port(p, root_port))
148 continue;
149
150 if (p->flags & BR_ROOT_BLOCK)
151 br_root_port_block(br, p);
152 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 root_port = p->port_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
156 br->root_port = root_port;
157
158 if (!root_port) {
159 br->designated_root = br->bridge_id;
160 br->root_path_cost = 0;
161 } else {
162 p = br_get_port(br, root_port);
163 br->designated_root = p->designated_root;
164 br->root_path_cost = p->designated_cost + p->path_cost;
165 }
166}
167
168/* called under bridge lock */
169void br_become_root_bridge(struct net_bridge *br)
170{
171 br->max_age = br->bridge_max_age;
172 br->hello_time = br->bridge_hello_time;
173 br->forward_delay = br->bridge_forward_delay;
174 br_topology_change_detection(br);
175 del_timer(&br->tcn_timer);
176
177 if (br->dev->flags & IFF_UP) {
178 br_config_bpdu_generation(br);
179 mod_timer(&br->hello_timer, jiffies + br->hello_time);
180 }
181}
182
183/* called under bridge lock */
184void br_transmit_config(struct net_bridge_port *p)
185{
186 struct br_config_bpdu bpdu;
187 struct net_bridge *br;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (timer_pending(&p->hold_timer)) {
190 p->config_pending = 1;
191 return;
192 }
193
194 br = p->br;
195
196 bpdu.topology_change = br->topology_change;
197 bpdu.topology_change_ack = p->topology_change_ack;
198 bpdu.root = br->designated_root;
199 bpdu.root_path_cost = br->root_path_cost;
200 bpdu.bridge_id = br->bridge_id;
201 bpdu.port_id = p->port_id;
202 if (br_is_root_bridge(br))
203 bpdu.message_age = 0;
204 else {
205 struct net_bridge_port *root
206 = br_get_port(br, br->root_port);
stephen hemminger0c031502011-07-22 07:47:06 +0000207 bpdu.message_age = (jiffies - root->designated_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 + MESSAGE_AGE_INCR;
209 }
210 bpdu.max_age = br->max_age;
211 bpdu.hello_time = br->hello_time;
212 bpdu.forward_delay = br->forward_delay;
213
214 if (bpdu.message_age < br->max_age) {
215 br_send_config_bpdu(p, &bpdu);
216 p->topology_change_ack = 0;
217 p->config_pending = 0;
Nikolay Aleksandrov76b91c32015-07-23 11:01:05 -0700218 if (p->br->stp_enabled == BR_KERNEL_STP)
219 mod_timer(&p->hold_timer,
220 round_jiffies(jiffies + BR_HOLD_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222}
223
224/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000225static void br_record_config_information(struct net_bridge_port *p,
226 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 p->designated_root = bpdu->root;
229 p->designated_cost = bpdu->root_path_cost;
230 p->designated_bridge = bpdu->bridge_id;
231 p->designated_port = bpdu->port_id;
Joakim Tjernlund709e1b52012-03-01 08:12:19 +0000232 p->designated_age = jiffies - bpdu->message_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900234 mod_timer(&p->message_age_timer, jiffies
Chris Healy9a062012013-09-11 21:37:47 -0700235 + (bpdu->max_age - bpdu->message_age));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000239static void br_record_config_timeout_values(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 const struct br_config_bpdu *bpdu)
241{
242 br->max_age = bpdu->max_age;
243 br->hello_time = bpdu->hello_time;
244 br->forward_delay = bpdu->forward_delay;
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500245 __br_set_topology_change(br, bpdu->topology_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/* called under bridge lock */
249void br_transmit_tcn(struct net_bridge *br)
250{
stephen hemminger91bc0332013-04-30 05:29:27 +0000251 struct net_bridge_port *p;
252
253 p = br_get_port(br, br->root_port);
254 if (p)
255 br_send_tcn_bpdu(p);
256 else
257 br_notice(br, "root port %u not found for topology notice\n",
258 br->root_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/* called under bridge lock */
262static int br_should_become_designated_port(const struct net_bridge_port *p)
263{
264 struct net_bridge *br;
265 int t;
266
267 br = p->br;
268 if (br_is_designated_port(p))
269 return 1;
270
271 if (memcmp(&p->designated_root, &br->designated_root, 8))
272 return 1;
273
274 if (br->root_path_cost < p->designated_cost)
275 return 1;
276 else if (br->root_path_cost > p->designated_cost)
277 return 0;
278
279 t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
280 if (t < 0)
281 return 1;
282 else if (t > 0)
283 return 0;
284
285 if (p->port_id < p->designated_port)
286 return 1;
287
288 return 0;
289}
290
291/* called under bridge lock */
292static void br_designated_port_selection(struct net_bridge *br)
293{
294 struct net_bridge_port *p;
295
296 list_for_each_entry(p, &br->port_list, list) {
297 if (p->state != BR_STATE_DISABLED &&
298 br_should_become_designated_port(p))
299 br_become_designated_port(p);
300
301 }
302}
303
304/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000305static int br_supersedes_port_info(const struct net_bridge_port *p,
306 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int t;
309
310 t = memcmp(&bpdu->root, &p->designated_root, 8);
311 if (t < 0)
312 return 1;
313 else if (t > 0)
314 return 0;
315
316 if (bpdu->root_path_cost < p->designated_cost)
317 return 1;
318 else if (bpdu->root_path_cost > p->designated_cost)
319 return 0;
320
321 t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
322 if (t < 0)
323 return 1;
324 else if (t > 0)
325 return 0;
326
327 if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
328 return 1;
329
330 if (bpdu->port_id <= p->designated_port)
331 return 1;
332
333 return 0;
334}
335
336/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000337static void br_topology_change_acknowledged(struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 br->topology_change_detected = 0;
340 del_timer(&br->tcn_timer);
341}
342
343/* called under bridge lock */
344void br_topology_change_detection(struct net_bridge *br)
345{
346 int isroot = br_is_root_bridge(br);
347
Stephen Hemminger4f0611a2009-05-15 06:11:58 +0000348 if (br->stp_enabled != BR_KERNEL_STP)
349 return;
350
stephen hemminger28a16c92010-05-10 09:31:09 +0000351 br_info(br, "topology change detected, %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 isroot ? "propagating" : "sending tcn bpdu");
353
354 if (isroot) {
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500355 __br_set_topology_change(br, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 mod_timer(&br->topology_change_timer, jiffies
357 + br->bridge_forward_delay + br->bridge_max_age);
358 } else if (!br->topology_change_detected) {
359 br_transmit_tcn(br);
360 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
361 }
362
363 br->topology_change_detected = 1;
364}
365
366/* called under bridge lock */
367void br_config_bpdu_generation(struct net_bridge *br)
368{
369 struct net_bridge_port *p;
370
371 list_for_each_entry(p, &br->port_list, list) {
372 if (p->state != BR_STATE_DISABLED &&
373 br_is_designated_port(p))
374 br_transmit_config(p);
375 }
376}
377
378/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000379static void br_reply(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 br_transmit_config(p);
382}
383
384/* called under bridge lock */
385void br_configuration_update(struct net_bridge *br)
386{
387 br_root_selection(br);
388 br_designated_port_selection(br);
389}
390
391/* called under bridge lock */
392void br_become_designated_port(struct net_bridge_port *p)
393{
394 struct net_bridge *br;
395
396 br = p->br;
397 p->designated_root = br->designated_root;
398 p->designated_cost = br->root_path_cost;
399 p->designated_bridge = br->bridge_id;
400 p->designated_port = p->port_id;
401}
402
403
404/* called under bridge lock */
405static void br_make_blocking(struct net_bridge_port *p)
406{
407 if (p->state != BR_STATE_DISABLED &&
408 p->state != BR_STATE_BLOCKING) {
409 if (p->state == BR_STATE_FORWARDING ||
410 p->state == BR_STATE_LEARNING)
411 br_topology_change_detection(p->br);
412
Florian Fainelli775dd692014-09-30 16:13:19 -0700413 br_set_state(p, BR_STATE_BLOCKING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200414 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger4ecb9612011-07-22 07:47:09 +0000415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 del_timer(&p->forward_delay_timer);
417 }
418}
419
420/* called under bridge lock */
421static void br_make_forwarding(struct net_bridge_port *p)
422{
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700423 struct net_bridge *br = p->br;
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700424
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700425 if (p->state != BR_STATE_BLOCKING)
426 return;
427
stephen hemmingera461c022011-03-10 05:57:04 +0000428 if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
Florian Fainelli775dd692014-09-30 16:13:19 -0700429 br_set_state(p, BR_STATE_FORWARDING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700430 br_topology_change_detection(br);
431 del_timer(&p->forward_delay_timer);
stephen hemminger160d73b2011-07-22 07:47:10 +0000432 } else if (br->stp_enabled == BR_KERNEL_STP)
Florian Fainelli775dd692014-09-30 16:13:19 -0700433 br_set_state(p, BR_STATE_LISTENING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700434 else
Florian Fainelli775dd692014-09-30 16:13:19 -0700435 br_set_state(p, BR_STATE_LEARNING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700436
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200437 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700438
439 if (br->forward_delay != 0)
440 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443/* called under bridge lock */
444void br_port_state_selection(struct net_bridge *br)
445{
446 struct net_bridge_port *p;
stephen hemminger1faa4352011-03-07 08:34:06 +0000447 unsigned int liveports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1faa4352011-03-07 08:34:06 +0000450 if (p->state == BR_STATE_DISABLED)
451 continue;
452
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000453 /* Don't change port states if userspace is handling STP */
454 if (br->stp_enabled != BR_USER_STP) {
455 if (p->port_no == br->root_port) {
456 p->config_pending = 0;
457 p->topology_change_ack = 0;
458 br_make_forwarding(p);
459 } else if (br_is_designated_port(p)) {
460 del_timer(&p->message_age_timer);
461 br_make_forwarding(p);
462 } else {
463 p->config_pending = 0;
464 p->topology_change_ack = 0;
465 br_make_blocking(p);
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
Nikolay Aleksandrov7ce42de2015-06-19 01:45:50 -0700469 if (p->state != BR_STATE_BLOCKING)
470 br_multicast_enable_port(p);
Nikolay Aleksandrov9aa66382015-06-23 04:47:44 -0700471 /* Multicast is not disabled for the port when it goes in
472 * blocking state because the timers will expire and stop by
473 * themselves without sending more queries.
474 */
stephen hemminger1faa4352011-03-07 08:34:06 +0000475 if (p->state == BR_STATE_FORWARDING)
476 ++liveports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
stephen hemminger1faa4352011-03-07 08:34:06 +0000478
479 if (liveports == 0)
480 netif_carrier_off(br->dev);
481 else
482 netif_carrier_on(br->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000486static void br_topology_change_acknowledge(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 p->topology_change_ack = 1;
489 br_transmit_config(p);
490}
491
492/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000493void br_received_config_bpdu(struct net_bridge_port *p,
494 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct net_bridge *br;
497 int was_root;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900498
Vivien Didelotde179962019-12-11 20:07:10 -0500499 p->stp_xstats.rx_bpdu++;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 br = p->br;
502 was_root = br_is_root_bridge(br);
503
504 if (br_supersedes_port_info(p, bpdu)) {
505 br_record_config_information(p, bpdu);
506 br_configuration_update(br);
507 br_port_state_selection(br);
508
509 if (!br_is_root_bridge(br) && was_root) {
510 del_timer(&br->hello_timer);
511 if (br->topology_change_detected) {
512 del_timer(&br->topology_change_timer);
513 br_transmit_tcn(br);
514
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900515 mod_timer(&br->tcn_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 jiffies + br->bridge_hello_time);
517 }
518 }
519
520 if (p->port_no == br->root_port) {
521 br_record_config_timeout_values(br, bpdu);
522 br_config_bpdu_generation(br);
523 if (bpdu->topology_change_ack)
524 br_topology_change_acknowledged(br);
525 }
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900526 } else if (br_is_designated_port(p)) {
527 br_reply(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529}
530
531/* called under bridge lock */
532void br_received_tcn_bpdu(struct net_bridge_port *p)
533{
Vivien Didelotde179962019-12-11 20:07:10 -0500534 p->stp_xstats.rx_tcn++;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (br_is_designated_port(p)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000537 br_info(p->br, "port %u(%s) received tcn bpdu\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000538 (unsigned int) p->port_no, p->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 br_topology_change_detection(p->br);
541 br_topology_change_acknowledge(p);
542 }
543}
stephen hemminger14f98f22011-04-04 14:03:33 +0000544
545/* Change bridge STP parameter */
546int br_set_hello_time(struct net_bridge *br, unsigned long val)
547{
548 unsigned long t = clock_t_to_jiffies(val);
549
550 if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
551 return -ERANGE;
552
553 spin_lock_bh(&br->lock);
554 br->bridge_hello_time = t;
555 if (br_is_root_bridge(br))
556 br->hello_time = br->bridge_hello_time;
557 spin_unlock_bh(&br->lock);
558 return 0;
559}
560
561int br_set_max_age(struct net_bridge *br, unsigned long val)
562{
563 unsigned long t = clock_t_to_jiffies(val);
564
565 if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
566 return -ERANGE;
567
568 spin_lock_bh(&br->lock);
569 br->bridge_max_age = t;
570 if (br_is_root_bridge(br))
571 br->max_age = br->bridge_max_age;
572 spin_unlock_bh(&br->lock);
573 return 0;
574
575}
576
Vivien Didelot82dd4332016-12-10 13:44:27 -0500577/* called under bridge lock */
578int __set_ageing_time(struct net_device *dev, unsigned long t)
579{
580 struct switchdev_attr attr = {
581 .orig_dev = dev,
582 .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
583 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
584 .u.ageing_time = jiffies_to_clock_t(t),
585 };
586 int err;
587
588 err = switchdev_port_attr_set(dev, &attr);
589 if (err && err != -EOPNOTSUPP)
590 return err;
591
592 return 0;
593}
594
Stephen Hemminger4c656c12016-03-08 12:59:35 -0800595/* Set time interval that dynamic forwarding entries live
596 * For pure software bridge, allow values outside the 802.1
597 * standard specification for special cases:
598 * 0 - entry never ages (all permanant)
599 * 1 - entry disappears (no persistance)
600 *
601 * Offloaded switch entries maybe more restrictive
602 */
Vivien Didelot9e0b27f2016-07-21 12:42:19 -0400603int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700604{
Scott Feldmanc62987b2015-10-08 19:23:19 -0700605 unsigned long t = clock_t_to_jiffies(ageing_time);
606 int err;
607
Vivien Didelot82dd4332016-12-10 13:44:27 -0500608 err = __set_ageing_time(br->dev, t);
609 if (err)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700610 return err;
611
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500612 spin_lock_bh(&br->lock);
613 br->bridge_ageing_time = t;
Scott Feldmanc62987b2015-10-08 19:23:19 -0700614 br->ageing_time = t;
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500615 spin_unlock_bh(&br->lock);
616
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100617 mod_delayed_work(system_long_wq, &br->gc_work, 0);
Scott Feldmanc62987b2015-10-08 19:23:19 -0700618
619 return 0;
620}
621
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500622/* called under bridge lock */
623void __br_set_topology_change(struct net_bridge *br, unsigned char val)
624{
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500625 unsigned long t;
626 int err;
627
628 if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
629 /* On topology change, set the bridge ageing time to twice the
630 * forward delay. Otherwise, restore its default ageing time.
631 */
632
633 if (val) {
634 t = 2 * br->forward_delay;
635 br_debug(br, "decreasing ageing time to %lu\n", t);
636 } else {
637 t = br->bridge_ageing_time;
638 br_debug(br, "restoring ageing time to %lu\n", t);
639 }
640
641 err = __set_ageing_time(br->dev, t);
642 if (err)
643 br_warn(br, "error offloading ageing time\n");
644 else
645 br->ageing_time = t;
646 }
647
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500648 br->topology_change = val;
649}
650
Herbert Xube4f1542013-09-12 17:12:05 +1000651void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
stephen hemminger14f98f22011-04-04 14:03:33 +0000652{
stephen hemminger14f98f22011-04-04 14:03:33 +0000653 br->bridge_forward_delay = t;
654 if (br_is_root_bridge(br))
655 br->forward_delay = br->bridge_forward_delay;
Herbert Xube4f1542013-09-12 17:12:05 +1000656}
657
658int br_set_forward_delay(struct net_bridge *br, unsigned long val)
659{
660 unsigned long t = clock_t_to_jiffies(val);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500661 int err = -ERANGE;
Herbert Xube4f1542013-09-12 17:12:05 +1000662
663 spin_lock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500664 if (br->stp_enabled != BR_NO_STP &&
665 (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
666 goto unlock;
667
Herbert Xube4f1542013-09-12 17:12:05 +1000668 __br_set_forward_delay(br, t);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500669 err = 0;
670
671unlock:
stephen hemminger14f98f22011-04-04 14:03:33 +0000672 spin_unlock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500673 return err;
stephen hemminger14f98f22011-04-04 14:03:33 +0000674}