forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrstp-state-machines.c
2218 lines (2085 loc) · 76.3 KB
/
rstp-state-machines.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011-2014 M3S, Srl - Italy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Rapid Spanning Tree Protocol (IEEE 802.1D-2004) state machines
* implementation.
*
* Authors:
* Martino Fornasa <[email protected]>
* Daniele Venturino <[email protected]>
*
* References to IEEE 802.1D-2004 standard are enclosed in square brackets.
* E.g. [17.3], [Table 17-1], etc.
*
*/
#include <config.h>
#include "rstp.h"
#include "rstp-state-machines.h"
#include <arpa/inet.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/types.h>
#include "byte-order.h"
#include "connectivity.h"
#include "ofpbuf.h"
#include "dp-packet.h"
#include "packets.h"
#include "seq.h"
#include "unixctl.h"
#include "util.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(rstp_sm);
#define ROLE_FLAG_MASK 0xC
#define ROLE_FLAG_SHIFT 2
enum port_flag {
PORT_UNKN = 0,
PORT_ALT_BACK = 1,
PORT_ROOT = 2,
PORT_DES = 3
};
enum bpdu_size {
CONFIGURATION_BPDU_SIZE = 35,
TOPOLOGY_CHANGE_NOTIFICATION_BPDU_SIZE = 4,
RAPID_SPANNING_TREE_BPDU_SIZE = 36
};
/* Same is a subset of SUPERIOR, so can be used as a boolean when the
* distinction is not significant. */
enum vector_comparison {
INFERIOR = 0,
SUPERIOR = 1,
SAME = 2
};
static void decrement_timer(uint16_t *);
static void rstp_send_bpdu(struct rstp_port *, const void *, size_t)
OVS_REQUIRES(rstp_mutex);
static int validate_received_bpdu(struct rstp_port *, const void *, size_t)
OVS_REQUIRES(rstp_mutex);
static ovs_be16 time_encode(uint8_t);
static uint8_t time_decode(ovs_be16);
static enum vector_comparison
compare_rstp_priority_vectors(const struct rstp_priority_vector *,
const struct rstp_priority_vector *);
static bool rstp_times_equal(struct rstp_times *, struct rstp_times *);
/* Per-Bridge State Machine */
static int port_role_selection_sm(struct rstp *)
OVS_REQUIRES(rstp_mutex);
/* Per-Port State Machines */
static int port_receive_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_protocol_migration_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int bridge_detection_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_transmit_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_information_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_role_transition_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_state_transition_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int topology_change_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
/* port_timers_sm() not defined as a state machine */
void
process_received_bpdu__(struct rstp_port *p, const void *bpdu_,
size_t bpdu_size)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *rstp = p->rstp;
struct rstp_bpdu *bpdu = (struct rstp_bpdu *)bpdu_;
if (!p->port_enabled) {
return;
}
if (p->rcvd_bpdu) {
return;
}
/* [9.2.9 Encoding of Port Role values]
* NOTE. If the Unknown value of the Port Role parameter is received, the
* state machines will effectively treat the RST BPDU as if it were a
* Configuration BPDU.
*/
if (bpdu->bpdu_type == RAPID_SPANNING_TREE_BPDU) {
uint8_t role = (bpdu->flags & ROLE_FLAG_MASK) >> ROLE_FLAG_SHIFT;
if (role == PORT_UNKN) {
bpdu->bpdu_type = CONFIGURATION_BPDU;
}
}
if (validate_received_bpdu(p, bpdu, bpdu_size) == 0) {
p->rcvd_bpdu = true;
p->rx_rstp_bpdu_cnt++;
memcpy(&p->received_bpdu_buffer, bpdu, sizeof(struct rstp_bpdu));
rstp->changes = true;
move_rstp__(rstp);
} else {
VLOG_DBG("%s, port %u: Bad STP or RSTP BPDU received", p->rstp->name,
p->port_number);
p->error_count++;
}
}
/* Returns 0 on success. */
static int
validate_received_bpdu(struct rstp_port *p, const void *bpdu, size_t bpdu_size)
OVS_REQUIRES(rstp_mutex)
{
/* Validation of received BPDU, see [9.3.4]. */
const struct rstp_bpdu *temp;
temp = bpdu;
if (bpdu_size < TOPOLOGY_CHANGE_NOTIFICATION_BPDU_SIZE ||
ntohs(temp->protocol_identifier) != 0) {
return -1;
} else {
if (temp->bpdu_type == CONFIGURATION_BPDU
&& bpdu_size >= CONFIGURATION_BPDU_SIZE
&& (time_decode(temp->message_age) < time_decode(temp->max_age))) {
if ((ntohll(temp->designated_bridge_id) !=
p->rstp->bridge_identifier)
|| ((ntohll(temp->designated_bridge_id) ==
p->rstp->bridge_identifier)
&& (ntohs(temp->designated_port_id) != p->port_id))) {
return 0;
} else {
return -1;
}
} else if (temp->bpdu_type == TOPOLOGY_CHANGE_NOTIFICATION_BPDU) {
return 0;
} else if (temp->bpdu_type == RAPID_SPANNING_TREE_BPDU &&
bpdu_size >= RAPID_SPANNING_TREE_BPDU_SIZE) {
return 0;
} else {
return -1;
}
}
}
/*
* move_rstp__()
* This method is invoked to move the State Machines. The SMs move only if the
* boolean 'changes' is true, meaning that something changed and the SMs need
* to work to process this change.
* The boolean 'changes' is set every time a SM modifies its state, a BPDU is
* received, a timer expires or port down event is detected. If a parameter is
* set by management, then 'changes' is set.
*/
#define MAX_RSTP_ITERATIONS 1000 /* safeguard */
int
move_rstp__(struct rstp *rstp)
OVS_REQUIRES(rstp_mutex)
{
int num_iterations;
num_iterations = 0;
while (rstp->changes == true && num_iterations < MAX_RSTP_ITERATIONS) {
struct rstp_port *p;
VLOG_DBG("%s: move_rstp()", rstp->name);
rstp->changes = false;
port_role_selection_sm(rstp);
HMAP_FOR_EACH (p, node, &rstp->ports) {
if (p->rstp_state != RSTP_DISABLED) {
port_receive_sm(p);
bridge_detection_sm(p);
port_information_sm(p);
port_role_transition_sm(p);
port_state_transition_sm(p);
topology_change_sm(p);
port_transmit_sm(p);
port_protocol_migration_sm(p);
}
}
num_iterations++;
seq_change(connectivity_seq_get());
}
if (num_iterations >= MAX_RSTP_ITERATIONS) {
VLOG_ERR("%s: move_rstp() reached the iteration safeguard limit!",
rstp->name);
}
return 0;
}
void decrease_rstp_port_timers__(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
decrement_timer(&p->hello_when);
decrement_timer(&p->tc_while);
decrement_timer(&p->fd_while);
decrement_timer(&p->rcvd_info_while);
decrement_timer(&p->rr_while);
decrement_timer(&p->rb_while);
decrement_timer(&p->mdelay_while);
decrement_timer(&p->edge_delay_while);
decrement_timer(&p->tx_count);
p->uptime += 1;
}
r->changes = true;
move_rstp__(r);
}
static void
decrement_timer(uint16_t *timer)
{
if (*timer != 0) {
*timer -= 1;
}
}
/* Bridge State Machine. */
/* [17.28] Port Role Selection state machine. */
static void
updt_role_disabled_tree(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
p->selected_role = ROLE_DISABLED;
}
}
static void
clear_reselect_tree(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
p->reselect = false;
}
}
void
updt_roles_tree__(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
int vsel;
struct rstp_priority_vector best_vector, candidate_vector;
enum rstp_port_role new_root_old_role = ROLE_DESIGNATED;
uint16_t old_root_port_number = 0;
uint16_t new_root_port_number = 0;
old_root_port_number = r->root_port_id & 0x00ff;
if (old_root_port_number) {
r->old_root_aux = rstp_get_port_aux__(r, old_root_port_number);
}
vsel = -1;
best_vector = r->bridge_priority;
/* Letter c1) */
r->root_times = r->bridge_times;
/* Letters a) b) c) */
HMAP_FOR_EACH (p, node, &r->ports) {
uint32_t old_root_path_cost;
uint32_t root_path_cost;
if (p->info_is != INFO_IS_RECEIVED) {
continue;
}
/* [17.6] */
candidate_vector = p->port_priority;
candidate_vector.bridge_port_id = p->port_id;
old_root_path_cost = candidate_vector.root_path_cost;
root_path_cost = old_root_path_cost + p->port_path_cost;
candidate_vector.root_path_cost = root_path_cost;
if ((candidate_vector.designated_bridge_id & 0xffffffffffffULL) ==
(r->bridge_priority.designated_bridge_id & 0xffffffffffffULL)) {
continue;
}
if (compare_rstp_priority_vectors(&candidate_vector,
&best_vector) == SUPERIOR) {
best_vector = candidate_vector;
r->root_times = p->port_times;
r->root_times.message_age++;
vsel = p->port_number;
new_root_old_role = p->role;
}
}
r->root_priority = best_vector;
r->root_port_id = best_vector.bridge_port_id;
VLOG_DBG("%s: new Root is "RSTP_ID_FMT, r->name,
RSTP_ID_ARGS(r->root_priority.root_bridge_id));
new_root_port_number = r->root_port_id & 0x00ff;
if (new_root_port_number) {
r->new_root_aux = rstp_get_port_aux__(r, new_root_port_number);
}
/* Shift learned MAC addresses from an old Root Port to an existing
* Alternate Port. */
if (!r->root_changed
&& new_root_old_role == ROLE_ALTERNATE
&& new_root_port_number
&& old_root_port_number
&& new_root_port_number != old_root_port_number) {
r->root_changed = true;
}
/* Letters d) e) */
HMAP_FOR_EACH (p, node, &r->ports) {
p->designated_priority_vector.root_bridge_id =
r->root_priority.root_bridge_id;
p->designated_priority_vector.root_path_cost =
r->root_priority.root_path_cost;
p->designated_priority_vector.designated_bridge_id =
r->bridge_identifier;
p->designated_priority_vector.designated_port_id =
p->port_id;
p->designated_times = r->root_times;
p->designated_times.hello_time = r->bridge_times.hello_time;
}
HMAP_FOR_EACH (p, node, &r->ports) {
switch (p->info_is) {
case INFO_IS_DISABLED:
p->selected_role = ROLE_DISABLED;
break;
case INFO_IS_AGED:
p->updt_info = true;
p->selected_role = ROLE_DESIGNATED;
break;
case INFO_IS_MINE:
p->selected_role = ROLE_DESIGNATED;
if (compare_rstp_priority_vectors(
&p->port_priority, &p->designated_priority_vector) != SAME
|| !rstp_times_equal(&p->designated_times, &r->root_times)) {
p->updt_info = true;
}
break;
case INFO_IS_RECEIVED:
if (vsel == p->port_number) { /* Letter i) */
p->selected_role = ROLE_ROOT;
p->updt_info = false;
} else if (compare_rstp_priority_vectors(
&p->designated_priority_vector,
&p->port_priority) == INFERIOR) {
if (p->port_priority.designated_bridge_id !=
r->bridge_identifier) {
p->selected_role = ROLE_ALTERNATE;
p->updt_info = false;
} else {
p->selected_role = ROLE_BACKUP;
p->updt_info = false;
}
} else {
p->selected_role = ROLE_DESIGNATED;
p->updt_info = true;
}
break;
default:
OVS_NOT_REACHED();
/* no break */
}
}
seq_change(connectivity_seq_get());
}
static void
set_selected_tree(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
if (p->reselect) {
return;
}
}
HMAP_FOR_EACH (p, node, &r->ports) {
p->selected = true;
}
}
static int
port_role_selection_sm(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
enum port_role_selection_state_machine old_state;
struct rstp_port *p;
old_state = r->port_role_selection_sm_state;
switch (r->port_role_selection_sm_state) {
case PORT_ROLE_SELECTION_SM_INIT:
if (r->begin) {
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_INIT_BRIDGE_EXEC;
}
break;
case PORT_ROLE_SELECTION_SM_INIT_BRIDGE_EXEC:
updt_role_disabled_tree(r);
r->port_role_selection_sm_state = PORT_ROLE_SELECTION_SM_INIT_BRIDGE;
/* no break */
case PORT_ROLE_SELECTION_SM_INIT_BRIDGE:
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_ROLE_SELECTION_EXEC;
break;
case PORT_ROLE_SELECTION_SM_ROLE_SELECTION_EXEC:
clear_reselect_tree(r);
updt_roles_tree__(r);
set_selected_tree(r);
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_ROLE_SELECTION;
/* no break */
case PORT_ROLE_SELECTION_SM_ROLE_SELECTION:
HMAP_FOR_EACH (p, node, &r->ports) {
if (p->reselect) {
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_ROLE_SELECTION_EXEC;
break;
}
}
break;
default:
OVS_NOT_REACHED();
/* no break */
}
if (old_state != r->port_role_selection_sm_state) {
r->changes = true;
VLOG_DBG("%s: Port_role_selection_sm %d -> %d", r->name,
old_state, r->port_role_selection_sm_state);
}
return 0;
}
/* Port State Machines */
/* [17.23 - Port receive state machine] */
static void
updt_bpdu_version(struct rstp_port *p) /* [17.21.22] */
OVS_REQUIRES(rstp_mutex)
{
switch (p->received_bpdu_buffer.bpdu_type) {
case CONFIGURATION_BPDU:
case TOPOLOGY_CHANGE_NOTIFICATION_BPDU:
p->rcvd_rstp = false;
p->rcvd_stp = true;
break;
case RAPID_SPANNING_TREE_BPDU:
p->rcvd_rstp = true;
p->rcvd_stp = false;
break;
default:
OVS_NOT_REACHED();
/* no break */
}
}
static int
port_receive_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_receive_state_machine old_state;
struct rstp *r;
old_state = p->port_receive_sm_state;
r = p->rstp;
switch (p->port_receive_sm_state) {
case PORT_RECEIVE_SM_INIT:
if (r->begin || ((p->rcvd_bpdu || (p->edge_delay_while !=
r->migrate_time)) && !p->port_enabled)) {
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD_EXEC;
}
break;
case PORT_RECEIVE_SM_DISCARD_EXEC:
p->rcvd_bpdu = p->rcvd_rstp = p->rcvd_stp = false;
p->rcvd_msg = false;
p->edge_delay_while = r->migrate_time;
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD;
/* no break */
case PORT_RECEIVE_SM_DISCARD:
if ((p->rcvd_bpdu || (p->edge_delay_while != r->migrate_time))
&& !p->port_enabled) {
/* Global transition. */
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD_EXEC;
} else if (p->rcvd_bpdu && p->port_enabled) {
p->port_receive_sm_state = PORT_RECEIVE_SM_RECEIVE_EXEC;
}
break;
case PORT_RECEIVE_SM_RECEIVE_EXEC:
updt_bpdu_version(p);
p->oper_edge = p->rcvd_bpdu = false;
p->rcvd_msg = true;
p->edge_delay_while = r->migrate_time;
p->port_receive_sm_state = PORT_RECEIVE_SM_RECEIVE;
/* no break */
case PORT_RECEIVE_SM_RECEIVE:
if ((p->rcvd_bpdu || (p->edge_delay_while != r->migrate_time))
&& !p->port_enabled) {
/* Global transition. */
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD_EXEC;
} else if (p->rcvd_bpdu && p->port_enabled && !p->rcvd_msg) {
p->port_receive_sm_state = PORT_RECEIVE_SM_RECEIVE_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* no break */
}
if (old_state != p->port_receive_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: Port_receive_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->port_receive_sm_state);
}
return 0;
}
/* [17.24 - Port Protocol Migration state machine] */
static int
port_protocol_migration_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_protocol_migration_state_machine old_state;
struct rstp *r;
old_state = p->port_protocol_migration_sm_state;
r = p->rstp;
switch (p->port_protocol_migration_sm_state) {
case PORT_PROTOCOL_MIGRATION_SM_INIT:
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC;
/* no break */
case PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC:
p->mcheck = false;
p->send_rstp = r->rstp_version;
p->mdelay_while = r->migrate_time;
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP;
/* no break */
case PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP:
if (p->mdelay_while == 0) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SENSING_EXEC;
} else if ((p->mdelay_while != r->migrate_time) && !p->port_enabled) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC;
}
break;
case PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP_EXEC:
p->send_rstp = false;
p->mdelay_while = r->migrate_time;
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP;
/* no break */
case PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP:
if ((p->mdelay_while == 0) || (!p->port_enabled) || p->mcheck) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SENSING_EXEC;
}
break;
case PORT_PROTOCOL_MIGRATION_SM_SENSING_EXEC:
p->rcvd_rstp = false;
p->rcvd_stp = false;
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SENSING;
/* no break */
case PORT_PROTOCOL_MIGRATION_SM_SENSING:
if (!p->port_enabled || p->mcheck || ((r->rstp_version) &&
!p->send_rstp && p->rcvd_rstp)) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC;
} else if (p->send_rstp && p->rcvd_stp) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* no break */
}
if (old_state != p->port_protocol_migration_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: port_protocol_migration_sm %d -> %d",
p->rstp->name, p->port_number, old_state,
p->port_protocol_migration_sm_state);
}
return 0;
}
/* [17.25 - Bridge Detection state machine] */
static int
bridge_detection_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum bridge_detection_state_machine old_state;
struct rstp *r;
old_state = p->bridge_detection_sm_state;
r = p->rstp;
switch (p->bridge_detection_sm_state) {
case BRIDGE_DETECTION_SM_INIT:
if (r->begin && p->admin_edge) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_EDGE_EXEC;
} else if (r->begin && !p->admin_edge) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_NOT_EDGE_EXEC;
}
break;
case BRIDGE_DETECTION_SM_EDGE_EXEC:
p->oper_edge = true;
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_EDGE;
/* no break */
case BRIDGE_DETECTION_SM_EDGE:
if ((!p->port_enabled && !p->admin_edge) || !p->oper_edge) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_NOT_EDGE_EXEC;
}
break;
case BRIDGE_DETECTION_SM_NOT_EDGE_EXEC:
p->oper_edge = false;
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_NOT_EDGE;
/* no break */
case BRIDGE_DETECTION_SM_NOT_EDGE:
if ((!p->port_enabled && p->admin_edge)
|| ((p->edge_delay_while == 0) && p->auto_edge && p->send_rstp
&& p->proposing)) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_EDGE_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* no break */
}
if (old_state != p->bridge_detection_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: bridge_detection_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->bridge_detection_sm_state);
}
return 0;
}
/* [17.26 - Port Transmit state machine] */
static void
rstp_send_bpdu(struct rstp_port *p, const void *bpdu, size_t bpdu_size)
OVS_REQUIRES(rstp_mutex)
{
struct eth_header *eth;
struct llc_header *llc;
struct dp_packet *pkt;
/* Skeleton. */
pkt = dp_packet_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
eth = dp_packet_put_zeros(pkt, sizeof *eth);
llc = dp_packet_put_zeros(pkt, sizeof *llc);
dp_packet_reset_offsets(pkt);
dp_packet_set_l3(pkt, dp_packet_put(pkt, bpdu, bpdu_size));
/* 802.2 header. */
eth->eth_dst = eth_addr_stp;
/* p->rstp->send_bpdu() must fill in source address. */
eth->eth_type = htons(dp_packet_size(pkt) - ETH_HEADER_LEN);
/* LLC header. */
llc->llc_dsap = STP_LLC_DSAP;
llc->llc_ssap = STP_LLC_SSAP;
llc->llc_cntl = STP_LLC_CNTL;
p->rstp->send_bpdu(pkt, p->aux, p->rstp->aux);
}
static void
record_agreement(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
r = p->rstp;
if (r->rstp_version && p->oper_point_to_point_mac &&
((p->received_bpdu_buffer.flags & BPDU_FLAG_AGREEMENT))) {
p->agreed = true;
p->proposing = false;
} else {
p->agreed = false;
}
}
static void
set_tc_flags(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* Sets rcvd_tc and/or rcvd_tc_ack if the Topology Change and/or Topology
* Change Acknowledgment flags, respectively, are set in a ConfigBPDU or
* RST BPDU.
*/
if (p->received_bpdu_buffer.bpdu_type == CONFIGURATION_BPDU ||
p->received_bpdu_buffer.bpdu_type == RAPID_SPANNING_TREE_BPDU) {
if ((p->received_bpdu_buffer.flags & BPDU_FLAG_TOPCHANGE) != 0) {
p->rcvd_tc = true;
}
if ((p->received_bpdu_buffer.flags & BPDU_FLAG_TOPCHANGEACK) != 0) {
p->rcvd_tc_ack = true;
}
}
/* Sets rcvd_tcn true if the BPDU is a TCN BPDU. */
if (p->received_bpdu_buffer.bpdu_type
== TOPOLOGY_CHANGE_NOTIFICATION_BPDU) {
p->rcvd_tcn = true;
}
}
static void
record_dispute(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
if ((p->received_bpdu_buffer.flags & BPDU_FLAG_LEARNING) != 0) {
/* 802.1D-2004 says to set the agreed flag and to clear the proposing
* flag. 802.1q-2008 instead says to set the disputed variable and to
* clear the agreed variable. */
p->disputed = true;
p->agreed = false;
}
}
static void
record_proposal(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_flag role =
((p->received_bpdu_buffer.flags) & ROLE_FLAG_MASK) >> ROLE_FLAG_SHIFT;
if ((role == PORT_DES)
&& ((p->received_bpdu_buffer.flags & BPDU_FLAG_PROPOSAL) != 0)) {
p->proposed = true;
}
}
static void
record_priority(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
p->port_priority.root_bridge_id = p->msg_priority.root_bridge_id;
p->port_priority.root_path_cost = p->msg_priority.root_path_cost;
p->port_priority.designated_bridge_id =
p->msg_priority.designated_bridge_id;
p->port_priority.designated_port_id = p->msg_priority.designated_port_id;
}
static void
record_times(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
p->port_times = p->msg_times;
if (p->msg_times.hello_time == 0) {
p->port_times.hello_time = 1;
}
}
static void
updt_rcvd_info_while(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* [17.21.23]
* The value assigned to rcvdInfoWhile is the three times the Hello Time,
* if Message Age, incremented by 1 second and rounded to the nearest whole
* second, does not exceed Max Age, and is zero otherwise.
*/
if (p->port_times.message_age < p->port_times.max_age) {
p->rcvd_info_while = p->port_times.hello_time * 3;
} else {
p->rcvd_info_while = 0;
}
}
/* Times are internally held in seconds, while the protocol uses 1/256 seconds.
* time_encode() is used to convert time values sent in bpdus, while
* time_decode() is used to convert time values received in bpdus.
*/
static ovs_be16
time_encode(uint8_t value)
{
return htons(value * 256);
}
static uint8_t
time_decode(ovs_be16 encoded)
{
return ntohs(encoded) / 256;
}
/* [17.21.19] */
static void
tx_config(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_bpdu bpdu;
bpdu.protocol_identifier = htons(0);
bpdu.protocol_version_identifier = 0;
bpdu.bpdu_type = CONFIGURATION_BPDU;
bpdu.root_bridge_id = htonll(p->designated_priority_vector.root_bridge_id);
bpdu.root_path_cost = htonl(p->designated_priority_vector.root_path_cost);
bpdu.designated_bridge_id =
htonll(p->designated_priority_vector.designated_bridge_id);
bpdu.designated_port_id =
htons(p->designated_priority_vector.designated_port_id);
bpdu.message_age = time_encode(p->designated_times.message_age);
bpdu.max_age = time_encode(p->designated_times.max_age);
bpdu.hello_time = time_encode(p->designated_times.hello_time);
bpdu.forward_delay = time_encode(p->designated_times.forward_delay);
bpdu.flags = 0;
if (p->tc_while != 0) {
bpdu.flags |= BPDU_FLAG_TOPCHANGE;
}
if (p->tc_ack != 0) {
bpdu.flags |= BPDU_FLAG_TOPCHANGEACK;
}
rstp_send_bpdu(p, &bpdu, sizeof(struct rstp_bpdu));
}
/* [17.21.20] */
static void
tx_rstp(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_bpdu bpdu;
bpdu.protocol_identifier = htons(0);
bpdu.protocol_version_identifier = 2;
bpdu.bpdu_type = RAPID_SPANNING_TREE_BPDU;
bpdu.root_bridge_id = htonll(p->designated_priority_vector.root_bridge_id);
bpdu.root_path_cost = htonl(p->designated_priority_vector.root_path_cost);
bpdu.designated_bridge_id =
htonll(p->designated_priority_vector.designated_bridge_id);
bpdu.designated_port_id =
htons(p->designated_priority_vector.designated_port_id);
bpdu.message_age = time_encode(p->designated_times.message_age);
bpdu.max_age = time_encode(p->designated_times.max_age);
bpdu.hello_time = time_encode(p->designated_times.hello_time);
bpdu.forward_delay = time_encode(p->designated_times.forward_delay);
bpdu.flags = 0;
switch (p->role) {
case ROLE_ROOT:
bpdu.flags = PORT_ROOT << ROLE_FLAG_SHIFT;
break;
case ROLE_DESIGNATED:
bpdu.flags = PORT_DES << ROLE_FLAG_SHIFT;
break;
case ROLE_ALTERNATE:
case ROLE_BACKUP:
bpdu.flags = PORT_ALT_BACK << ROLE_FLAG_SHIFT;
break;
case ROLE_DISABLED:
/* Should not happen! */
VLOG_ERR("%s transmitting bpdu in disabled role on port "
RSTP_PORT_ID_FMT, p->rstp->name, p->port_id);
break;
}
if (p->agree) {
bpdu.flags |= BPDU_FLAG_AGREEMENT;
}
if (p->proposing) {
bpdu.flags |= BPDU_FLAG_PROPOSAL;
}
if (p->tc_while != 0) {
bpdu.flags |= BPDU_FLAG_TOPCHANGE;
}
if (p->learning) {
bpdu.flags |= BPDU_FLAG_LEARNING;
}
if (p->forwarding) {
bpdu.flags |= BPDU_FLAG_FORWARDING;
}
bpdu.version1_length = 0;
rstp_send_bpdu(p, &bpdu, sizeof(struct rstp_bpdu));
}
/* [17.21.21] */
static void
tx_tcn(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_bpdu bpdu;
memset(&bpdu, 0, sizeof(struct rstp_bpdu));
bpdu.protocol_identifier = htons(0);
bpdu.protocol_version_identifier = 0;
bpdu.bpdu_type = TOPOLOGY_CHANGE_NOTIFICATION_BPDU;
rstp_send_bpdu(p, &bpdu, sizeof(struct rstp_bpdu));
}
static int
port_transmit_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_transmit_state_machine old_state;
struct rstp *r;
old_state = p->port_transmit_sm_state;
r = p->rstp;
switch (p->port_transmit_sm_state) {
case PORT_TRANSMIT_SM_INIT:
if (r->begin) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_INIT_EXEC;
}
break;
case PORT_TRANSMIT_SM_TRANSMIT_INIT_EXEC:
p->new_info = true;
p->tx_count = 0;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_INIT;
/* no break */
case PORT_TRANSMIT_SM_TRANSMIT_INIT:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
case PORT_TRANSMIT_SM_TRANSMIT_PERIODIC_EXEC:
p->new_info = p->new_info || (p->role == ROLE_DESIGNATED ||
(p->role == ROLE_ROOT && p->tc_while != 0));
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_PERIODIC;
/* no break */
case PORT_TRANSMIT_SM_TRANSMIT_PERIODIC:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
case PORT_TRANSMIT_SM_IDLE_EXEC:
p->hello_when = r->bridge_hello_time;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE;
/* no break */
case PORT_TRANSMIT_SM_IDLE:
if (p->role == ROLE_DISABLED) {
VLOG_DBG("%s, port %u: port_transmit_sm ROLE == DISABLED.",
p->rstp->name, p->port_number);
break;
} else if (p->send_rstp && p->new_info
&& p->tx_count < r->transmit_hold_count
&& p->hello_when != 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_RSTP_EXEC;
} else if (p->hello_when == 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state =
PORT_TRANSMIT_SM_TRANSMIT_PERIODIC_EXEC;
} else if (!p->send_rstp && p->new_info && p->role == ROLE_ROOT
&& p->tx_count < r->transmit_hold_count
&& p->hello_when != 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_TCN_EXEC;
} else if (!p->send_rstp && p->new_info && p->role == ROLE_DESIGNATED
&& p->tx_count < r->transmit_hold_count
&& p->hello_when != 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_CONFIG_EXEC;
}
break;
case PORT_TRANSMIT_SM_TRANSMIT_CONFIG_EXEC:
p->new_info = false;
tx_config(p);
p->tx_count += 1;
p->tc_ack = false;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_CONFIG;
/* no break */