]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_fsm.c
Merge pull request #1075 from donaldsharp/rip_metric
[mirror_frr.git] / eigrpd / eigrp_fsm.c
1 /*
2 * EIGRPd Finite State Machine (DUAL).
3 * Copyright (C) 2013-2014
4 * Authors:
5 * Donnie Savage
6 * Jan Janovic
7 * Matej Perina
8 * Peter Orsag
9 * Peter Paluch
10 *
11 * This file is part of GNU Zebra.
12 *
13 * GNU Zebra is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
16 * later version.
17 *
18 * GNU Zebra is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; see the file COPYING; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * This file contains functions for executing logic of finite state machine
28 *
29 * +------------ +
30 * | (7) |
31 * | v
32 * +=====================================+
33 * | |
34 * | Passive |
35 * | |
36 * +=====================================+
37 * ^ | ^ ^ ^ |
38 * (3)| | (1)| | (1)| |
39 * | (0)| | (3)| | (2)|
40 * | | | | | +---------------+
41 * | | | | | \
42 * +--------+ | | | +-----------------+ \
43 * / / / | \ \
44 * / / / +----+ \ \
45 * | | | | | |
46 * | v | | | v
47 * +===========+ (6) +===========+ +===========+ (6) +===========+
48 * | |------->| | (5) | |-------->| |
49 * | | (4) | |------>| | (4) | |
50 * | ACTIVE 0 |<-------| ACTIVE 1 | | ACTIVE 2 |<--------| ACTIVE 3
51 * |
52 * +--| | +--| | +--| | +--| |
53 * | +===========+ | +===========+ | +===========+ |
54 * +===========+
55 * | ^ |(5) | ^ | ^ ^ | ^
56 * | | +---------|------|------------|----+ | | |
57 * +-------+ +------+ +---------+ +---------+
58 * (7) (7) (7) (7)
59 *
60 * 0- input event other than query from successor, FC not satisfied
61 * 1- last reply, FD is reset
62 * 2- query from successor, FC not satisfied
63 * 3- last reply, FC satisfied with current value of FDij
64 * 4- distance increase while in active state
65 * 5- query from successor while in active state
66 * 6- last reply, FC not satisfied with current value of FDij
67 * 7- state not changed, usually by receiving not last reply
68 */
69
70 #include <thread.h>
71 #include <zebra.h>
72
73 #include "prefix.h"
74 #include "table.h"
75 #include "memory.h"
76 #include "log.h"
77 #include "linklist.h"
78 #include "vty.h"
79
80 #include "eigrpd/eigrp_structs.h"
81 #include "eigrpd/eigrpd.h"
82 #include "eigrpd/eigrp_interface.h"
83 #include "eigrpd/eigrp_neighbor.h"
84 #include "eigrpd/eigrp_packet.h"
85 #include "eigrpd/eigrp_zebra.h"
86 #include "eigrpd/eigrp_vty.h"
87 #include "eigrpd/eigrp_network.h"
88 #include "eigrpd/eigrp_dump.h"
89 #include "eigrpd/eigrp_topology.h"
90 #include "eigrpd/eigrp_fsm.h"
91
92 /*
93 * Prototypes
94 */
95 int eigrp_fsm_event_keep_state(struct eigrp_fsm_action_message *);
96 int eigrp_fsm_event_nq_fcn(struct eigrp_fsm_action_message *);
97 int eigrp_fsm_event_q_fcn(struct eigrp_fsm_action_message *);
98 int eigrp_fsm_event_lr(struct eigrp_fsm_action_message *);
99 int eigrp_fsm_event_dinc(struct eigrp_fsm_action_message *);
100 int eigrp_fsm_event_lr_fcs(struct eigrp_fsm_action_message *);
101 int eigrp_fsm_event_lr_fcn(struct eigrp_fsm_action_message *);
102 int eigrp_fsm_event_qact(struct eigrp_fsm_action_message *);
103
104 //---------------------------------------------------------------------
105
106 /*
107 * NSM - field of fields of struct containing one function each.
108 * Which function is used depends on actual state of FSM and occurred
109 * event(arrow in diagram). Usage:
110 * NSM[actual/starting state][occurred event].func
111 * Functions are should be executed within separate thread.
112 */
113 struct {
114 int (*func)(struct eigrp_fsm_action_message *);
115 } NSM[EIGRP_FSM_STATE_MAX][EIGRP_FSM_EVENT_MAX] = {
116 {
117 // PASSIVE STATE
118 {eigrp_fsm_event_nq_fcn}, /* Event 0 */
119 {eigrp_fsm_event_keep_state}, /* Event 1 */
120 {eigrp_fsm_event_q_fcn}, /* Event 2 */
121 {eigrp_fsm_event_keep_state}, /* Event 3 */
122 {eigrp_fsm_event_keep_state}, /* Event 4 */
123 {eigrp_fsm_event_keep_state}, /* Event 5 */
124 {eigrp_fsm_event_keep_state}, /* Event 6 */
125 {eigrp_fsm_event_keep_state}, /* Event 7 */
126 },
127 {
128 // Active 0 state
129 {eigrp_fsm_event_keep_state}, /* Event 0 */
130 {eigrp_fsm_event_keep_state}, /* Event 1 */
131 {eigrp_fsm_event_keep_state}, /* Event 2 */
132 {eigrp_fsm_event_lr_fcs}, /* Event 3 */
133 {eigrp_fsm_event_keep_state}, /* Event 4 */
134 {eigrp_fsm_event_qact}, /* Event 5 */
135 {eigrp_fsm_event_lr_fcn}, /* Event 6 */
136 {eigrp_fsm_event_keep_state}, /* Event 7 */
137 },
138 {
139 // Active 1 state
140 {eigrp_fsm_event_keep_state}, /* Event 0 */
141 {eigrp_fsm_event_lr}, /* Event 1 */
142 {eigrp_fsm_event_keep_state}, /* Event 2 */
143 {eigrp_fsm_event_keep_state}, /* Event 3 */
144 {eigrp_fsm_event_dinc}, /* Event 4 */
145 {eigrp_fsm_event_qact}, /* Event 5 */
146 {eigrp_fsm_event_keep_state}, /* Event 6 */
147 {eigrp_fsm_event_keep_state}, /* Event 7 */
148 },
149 {
150 // Active 2 state
151 {eigrp_fsm_event_keep_state}, /* Event 0 */
152 {eigrp_fsm_event_keep_state}, /* Event 1 */
153 {eigrp_fsm_event_keep_state}, /* Event 2 */
154 {eigrp_fsm_event_lr_fcs}, /* Event 3 */
155 {eigrp_fsm_event_keep_state}, /* Event 4 */
156 {eigrp_fsm_event_keep_state}, /* Event 5 */
157 {eigrp_fsm_event_lr_fcn}, /* Event 6 */
158 {eigrp_fsm_event_keep_state}, /* Event 7 */
159 },
160 {
161 // Active 3 state
162 {eigrp_fsm_event_keep_state}, /* Event 0 */
163 {eigrp_fsm_event_lr}, /* Event 1 */
164 {eigrp_fsm_event_keep_state}, /* Event 2 */
165 {eigrp_fsm_event_keep_state}, /* Event 3 */
166 {eigrp_fsm_event_dinc}, /* Event 4 */
167 {eigrp_fsm_event_keep_state}, /* Event 5 */
168 {eigrp_fsm_event_keep_state}, /* Event 6 */
169 {eigrp_fsm_event_keep_state}, /* Event 7 */
170 },
171 };
172
173 /*
174 * Main function in which are make decisions which event occurred.
175 * msg - argument of type struct eigrp_fsm_action_message contain
176 * details about what happen
177 *
178 * Return number of occurred event (arrow in diagram).
179 *
180 */
181 static int eigrp_get_fsm_event(struct eigrp_fsm_action_message *msg)
182 {
183 // Loading base information from message
184 // struct eigrp *eigrp = msg->eigrp;
185 struct eigrp_prefix_entry *prefix = msg->prefix;
186 struct eigrp_nexthop_entry *entry = msg->entry;
187 u_char actual_state = prefix->state;
188 enum metric_change change;
189
190 if (entry == NULL) {
191 entry = eigrp_nexthop_entry_new();
192 entry->adv_router = msg->adv_router;
193 entry->ei = msg->adv_router->ei;
194 entry->prefix = prefix;
195 msg->entry = entry;
196 }
197
198 /*
199 * Calculate resultant metrics and insert to correct position
200 * in entries list
201 */
202 change = eigrp_topology_update_distance(msg);
203
204 switch (actual_state) {
205 case EIGRP_FSM_STATE_PASSIVE: {
206 struct eigrp_nexthop_entry *head =
207 listnode_head(prefix->entries);
208
209 if (head->reported_distance < prefix->fdistance) {
210 return EIGRP_FSM_KEEP_STATE;
211 }
212 /*
213 * if best entry doesn't satisfy feasibility condition it means
214 * move to active state
215 * dependently if it was query from successor
216 */
217 if (msg->packet_type == EIGRP_OPC_QUERY) {
218 return EIGRP_FSM_EVENT_Q_FCN;
219 } else {
220 return EIGRP_FSM_EVENT_NQ_FCN;
221 }
222
223 break;
224 }
225 case EIGRP_FSM_STATE_ACTIVE_0: {
226 if (msg->packet_type == EIGRP_OPC_REPLY) {
227 struct eigrp_nexthop_entry *head =
228 listnode_head(prefix->entries);
229
230 listnode_delete(prefix->rij, entry->adv_router);
231 if (prefix->rij->count)
232 return EIGRP_FSM_KEEP_STATE;
233
234 zlog_info("All reply received\n");
235 if (head->reported_distance
236 < prefix->fdistance) {
237 return EIGRP_FSM_EVENT_LR_FCS;
238 }
239
240 return EIGRP_FSM_EVENT_LR_FCN;
241 } else if (msg->packet_type == EIGRP_OPC_QUERY
242 && (entry->flags
243 & EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG)) {
244 return EIGRP_FSM_EVENT_QACT;
245 }
246
247 return EIGRP_FSM_KEEP_STATE;
248
249 break;
250 }
251 case EIGRP_FSM_STATE_ACTIVE_1: {
252 if (msg->packet_type == EIGRP_OPC_QUERY
253 && (entry->flags & EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG)) {
254 return EIGRP_FSM_EVENT_QACT;
255 } else if (msg->packet_type == EIGRP_OPC_REPLY) {
256 listnode_delete(prefix->rij, entry->adv_router);
257
258 if (change == METRIC_INCREASE
259 && (entry->flags
260 & EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG)) {
261 return EIGRP_FSM_EVENT_DINC;
262 } else if (prefix->rij->count) {
263 return EIGRP_FSM_KEEP_STATE;
264 } else {
265 zlog_info("All reply received\n");
266 return EIGRP_FSM_EVENT_LR;
267 }
268 } else if (msg->packet_type == EIGRP_OPC_UPDATE && change == 1
269 && (entry->flags
270 & EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG)) {
271 return EIGRP_FSM_EVENT_DINC;
272 }
273 return EIGRP_FSM_KEEP_STATE;
274
275 break;
276 }
277 case EIGRP_FSM_STATE_ACTIVE_2: {
278 if (msg->packet_type == EIGRP_OPC_REPLY) {
279 struct eigrp_nexthop_entry *head =
280 listnode_head(prefix->entries);
281
282 listnode_delete(prefix->rij, entry->adv_router);
283 if (prefix->rij->count) {
284 return EIGRP_FSM_KEEP_STATE;
285 } else {
286 zlog_info("All reply received\n");
287 if (head->reported_distance
288 < prefix->fdistance) {
289 return EIGRP_FSM_EVENT_LR_FCS;
290 }
291
292 return EIGRP_FSM_EVENT_LR_FCN;
293 }
294 }
295 return EIGRP_FSM_KEEP_STATE;
296
297 break;
298 }
299 case EIGRP_FSM_STATE_ACTIVE_3: {
300 if (msg->packet_type == EIGRP_OPC_REPLY) {
301 listnode_delete(prefix->rij, entry->adv_router);
302
303 if (change == METRIC_INCREASE
304 && (entry->flags
305 & EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG)) {
306 return EIGRP_FSM_EVENT_DINC;
307 } else if (prefix->rij->count) {
308 return EIGRP_FSM_KEEP_STATE;
309 } else {
310 zlog_info("All reply received\n");
311 return EIGRP_FSM_EVENT_LR;
312 }
313 } else if (msg->packet_type == EIGRP_OPC_UPDATE && change == 1
314 && (entry->flags
315 & EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG)) {
316 return EIGRP_FSM_EVENT_DINC;
317 }
318 return EIGRP_FSM_KEEP_STATE;
319
320 break;
321 }
322 }
323
324 return EIGRP_FSM_KEEP_STATE;
325 }
326
327 /*
328 * Function made to execute in separate thread.
329 * Load argument from thread and execute proper NSM function
330 */
331 int eigrp_fsm_event(struct eigrp_fsm_action_message *msg)
332 {
333 int event = eigrp_get_fsm_event(msg);
334 zlog_info("EIGRP AS: %d State: %d Event: %d Network: %s",
335 msg->eigrp->AS, msg->prefix->state, event,
336 eigrp_topology_ip_string(msg->prefix));
337 (*(NSM[msg->prefix->state][event].func))(msg);
338
339 return 1;
340 }
341
342 /*
343 * Function of event 0.
344 *
345 */
346 int eigrp_fsm_event_nq_fcn(struct eigrp_fsm_action_message *msg)
347 {
348 struct eigrp *eigrp = msg->eigrp;
349 struct eigrp_prefix_entry *prefix = msg->prefix;
350 struct list *successors = eigrp_topology_get_successor(prefix);
351 struct eigrp_nexthop_entry *ne;
352
353 assert(successors); // If this is NULL we have shit the bed, fun huh?
354
355 ne = listnode_head(successors);
356 prefix->state = EIGRP_FSM_STATE_ACTIVE_1;
357 prefix->rdistance = prefix->distance = prefix->fdistance =
358 ne->distance;
359 prefix->reported_metric = ne->total_metric;
360
361 if (eigrp_nbr_count_get()) {
362 prefix->req_action |= EIGRP_FSM_NEED_QUERY;
363 listnode_add(eigrp->topology_changes_internalIPV4, prefix);
364 } else {
365 eigrp_fsm_event_lr(msg); // in the case that there are no more
366 // neighbors left
367 }
368
369 list_delete(successors);
370
371 return 1;
372 }
373
374 int eigrp_fsm_event_q_fcn(struct eigrp_fsm_action_message *msg)
375 {
376 struct eigrp *eigrp = msg->eigrp;
377 struct eigrp_prefix_entry *prefix = msg->prefix;
378 struct list *successors = eigrp_topology_get_successor(prefix);
379 struct eigrp_nexthop_entry *ne;
380
381 assert(successors); // If this is NULL somebody poked us in the eye.
382
383 ne = listnode_head(successors);
384 prefix->state = EIGRP_FSM_STATE_ACTIVE_3;
385 prefix->rdistance = prefix->distance = prefix->fdistance =
386 ne->distance;
387 prefix->reported_metric = ne->total_metric;
388 if (eigrp_nbr_count_get()) {
389 prefix->req_action |= EIGRP_FSM_NEED_QUERY;
390 listnode_add(eigrp->topology_changes_internalIPV4, prefix);
391 } else {
392 eigrp_fsm_event_lr(msg); // in the case that there are no more
393 // neighbors left
394 }
395
396 list_delete(successors);
397
398 return 1;
399 }
400
401 int eigrp_fsm_event_keep_state(struct eigrp_fsm_action_message *msg)
402 {
403 struct eigrp_prefix_entry *prefix = msg->prefix;
404 struct eigrp_nexthop_entry *ne = listnode_head(prefix->entries);
405
406 if (prefix->state == EIGRP_FSM_STATE_PASSIVE) {
407 if (!eigrp_metrics_is_same(prefix->reported_metric,
408 ne->total_metric)) {
409 prefix->rdistance = prefix->fdistance =
410 prefix->distance = ne->distance;
411 prefix->reported_metric =
412 ne->total_metric;
413 if (msg->packet_type == EIGRP_OPC_QUERY)
414 eigrp_send_reply(msg->adv_router, prefix);
415 prefix->req_action |= EIGRP_FSM_NEED_UPDATE;
416 listnode_add(
417 (eigrp_lookup())->topology_changes_internalIPV4,
418 prefix);
419 }
420 eigrp_topology_update_node_flags(prefix);
421 eigrp_update_routing_table(prefix);
422 }
423
424 if (msg->packet_type == EIGRP_OPC_QUERY)
425 eigrp_send_reply(msg->adv_router, prefix);
426
427 return 1;
428 }
429
430 int eigrp_fsm_event_lr(struct eigrp_fsm_action_message *msg)
431 {
432 struct eigrp *eigrp = msg->eigrp;
433 struct eigrp_prefix_entry *prefix = msg->prefix;
434 struct eigrp_nexthop_entry *ne = listnode_head(prefix->entries);
435
436 prefix->fdistance = prefix->distance = prefix->rdistance =
437 ne->distance;
438 prefix->reported_metric = ne->total_metric;
439
440 if (prefix->state == EIGRP_FSM_STATE_ACTIVE_3) {
441 struct list *successors = eigrp_topology_get_successor(prefix);
442
443 assert(successors); // It's like Napolean and Waterloo
444
445 ne = listnode_head(successors);
446 eigrp_send_reply(ne->adv_router,
447 prefix);
448 list_delete(successors);
449 }
450
451 prefix->state = EIGRP_FSM_STATE_PASSIVE;
452 prefix->req_action |= EIGRP_FSM_NEED_UPDATE;
453 listnode_add(eigrp->topology_changes_internalIPV4, prefix);
454 eigrp_topology_update_node_flags(prefix);
455 eigrp_update_routing_table(prefix);
456 eigrp_update_topology_table_prefix(eigrp->topology_table, prefix);
457
458 return 1;
459 }
460
461 int eigrp_fsm_event_dinc(struct eigrp_fsm_action_message *msg)
462 {
463 struct list *successors = eigrp_topology_get_successor(msg->prefix);
464 struct eigrp_nexthop_entry *ne;
465
466 assert(successors); // Trump and his big hands
467
468 ne = listnode_head(successors);
469 msg->prefix->state = msg->prefix->state == EIGRP_FSM_STATE_ACTIVE_1
470 ? EIGRP_FSM_STATE_ACTIVE_0
471 : EIGRP_FSM_STATE_ACTIVE_2;
472 msg->prefix->distance = ne->distance;
473 if (!msg->prefix->rij->count)
474 (*(NSM[msg->prefix->state][eigrp_get_fsm_event(msg)].func))(
475 msg);
476
477
478 list_delete(successors);
479 return 1;
480 }
481
482 int eigrp_fsm_event_lr_fcs(struct eigrp_fsm_action_message *msg)
483 {
484 struct eigrp *eigrp = msg->eigrp;
485 struct eigrp_prefix_entry *prefix = msg->prefix;
486 struct eigrp_nexthop_entry *ne = listnode_head(prefix->entries);
487
488 prefix->state = EIGRP_FSM_STATE_PASSIVE;
489 prefix->distance = prefix->rdistance = ne->distance;
490 prefix->reported_metric = ne->total_metric;
491 prefix->fdistance = prefix->fdistance > prefix->distance
492 ? prefix->distance
493 : prefix->fdistance;
494 if (prefix->state == EIGRP_FSM_STATE_ACTIVE_2) {
495 struct list *successors = eigrp_topology_get_successor(prefix);
496
497 assert(successors); // Having a spoon and all you need is a
498 // knife
499 ne = listnode_head(successors);
500 eigrp_send_reply(ne->adv_router,
501 prefix);
502
503 list_delete(successors);
504 }
505 prefix->req_action |= EIGRP_FSM_NEED_UPDATE;
506 listnode_add(eigrp->topology_changes_internalIPV4, prefix);
507 eigrp_topology_update_node_flags(prefix);
508 eigrp_update_routing_table(prefix);
509 eigrp_update_topology_table_prefix(eigrp->topology_table, prefix);
510
511 return 1;
512 }
513
514 int eigrp_fsm_event_lr_fcn(struct eigrp_fsm_action_message *msg)
515 {
516 struct eigrp *eigrp = msg->eigrp;
517 struct eigrp_prefix_entry *prefix = msg->prefix;
518 struct eigrp_nexthop_entry *best_successor;
519 struct list *successors = eigrp_topology_get_successor(prefix);
520
521 assert(successors); // Routing without a stack
522
523 prefix->state = prefix->state == EIGRP_FSM_STATE_ACTIVE_0
524 ? EIGRP_FSM_STATE_ACTIVE_1
525 : EIGRP_FSM_STATE_ACTIVE_3;
526
527 best_successor = listnode_head(successors);
528 prefix->rdistance = prefix->distance = best_successor->distance;
529 prefix->reported_metric = best_successor->total_metric;
530
531 if (eigrp_nbr_count_get()) {
532 prefix->req_action |= EIGRP_FSM_NEED_QUERY;
533 listnode_add(eigrp->topology_changes_internalIPV4, prefix);
534 } else {
535 eigrp_fsm_event_lr(msg); // in the case that there are no more
536 // neighbors left
537 }
538
539 list_delete(successors);
540
541 return 1;
542 }
543
544 int eigrp_fsm_event_qact(struct eigrp_fsm_action_message *msg)
545 {
546 struct list *successors = eigrp_topology_get_successor(msg->prefix);
547 struct eigrp_nexthop_entry *ne;
548
549 assert(successors); // Cats and no Dogs
550
551 ne = listnode_head(successors);
552 msg->prefix->state = EIGRP_FSM_STATE_ACTIVE_2;
553 msg->prefix->distance = ne->distance;
554
555 list_delete(successors);
556 return 1;
557 }