a842fbe428ce9b212918d70255695b6b0a888a5e
[sliver-openvswitch.git] / lib / lacp.c
1 /* Copyright (c) 2011 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "lacp.h"
18
19 #include <assert.h>
20 #include <stdlib.h>
21
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "hmap.h"
25 #include "ofpbuf.h"
26 #include "packets.h"
27 #include "poll-loop.h"
28 #include "timer.h"
29 #include "timeval.h"
30 #include "unixctl.h"
31 #include "vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(lacp);
34
35 enum slave_status {
36     LACP_CURRENT,   /* Current State.  Partner up to date. */
37     LACP_EXPIRED,   /* Expired State.  Partner out of date. */
38     LACP_DEFAULTED, /* Defaulted State.  No partner. */
39 };
40
41 struct lacp {
42     struct list node;             /* Node in all_lacps list. */
43     char *name;                   /* Name of this lacp object. */
44     uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
45     uint16_t sys_priority;        /* System Priority. */
46     bool active;                  /* Active or Passive. */
47
48     struct hmap slaves;      /* Slaves this LACP object controls. */
49     struct slave *key_slave; /* Slave whose ID will be the aggregation key. */
50
51     bool fast;                /* Fast or Slow LACP time. */
52     bool negotiated;         /* True if LACP negotiations were successful. */
53     bool update;             /* True if lacp_update() needs to be called. */
54 };
55
56 struct slave {
57     void *aux;                    /* Handle used to identify this slave. */
58     struct hmap_node node;        /* Node in master's slaves map. */
59
60     struct lacp *lacp;            /* LACP object containing this slave. */
61     uint16_t port_id;             /* Port ID. */
62     uint16_t port_priority;       /* Port Priority. */
63     char *name;                   /* Name of this slave. */
64
65     enum slave_status status;     /* Slave status. */
66     bool attached;                /* Attached. Traffic may flow. */
67     struct lacp_info partner;     /* Partner information. */
68     struct lacp_info ntt_actor;   /* Used to decide if we Need To Transmit. */
69     struct timer tx;              /* Next message transmission timer. */
70     struct timer rx;              /* Expected message receive timer. */
71 };
72
73 static struct list all_lacps = LIST_INITIALIZER(&all_lacps);
74
75 static void lacp_update_attached(struct lacp *);
76
77 static void slave_destroy(struct slave *);
78 static void slave_set_defaulted(struct slave *);
79 static void slave_set_expired(struct slave *);
80 static void slave_get_actor(struct slave *, struct lacp_info *actor);
81 static void slave_get_priority(struct slave *, struct lacp_info *priority);
82 static bool slave_may_tx(const struct slave *);
83 static struct slave *slave_lookup(const struct lacp *, const void *slave);
84 static bool info_tx_equal(struct lacp_info *, struct lacp_info *);
85
86 static void lacp_unixctl_show(struct unixctl_conn *, const char *args,
87                               void *aux);
88
89 /* Initializes the lacp module. */
90 void
91 lacp_init(void)
92 {
93     unixctl_command_register("lacp/show", lacp_unixctl_show, NULL);
94 }
95
96 /* Creates a LACP object. */
97 struct lacp *
98 lacp_create(void)
99 {
100     struct lacp *lacp;
101
102     lacp = xzalloc(sizeof *lacp);
103     hmap_init(&lacp->slaves);
104     list_push_back(&all_lacps, &lacp->node);
105     return lacp;
106 }
107
108 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
109 void
110 lacp_destroy(struct lacp *lacp)
111 {
112     if (lacp) {
113         struct slave *slave, *next;
114
115         HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
116             slave_destroy(slave);
117         }
118
119         hmap_destroy(&lacp->slaves);
120         list_remove(&lacp->node);
121         free(lacp->name);
122         free(lacp);
123     }
124 }
125
126 /* Configures 'lacp' with settings from 's'. */
127 void
128 lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
129 {
130     if (!lacp->name || strcmp(s->name, lacp->name)) {
131         free(lacp->name);
132         lacp->name = xstrdup(s->name);
133     }
134
135     if (!eth_addr_equals(lacp->sys_id, s->id)
136         || lacp->sys_priority != s->priority) {
137         memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
138         lacp->sys_priority = s->priority;
139         lacp->update = true;
140     }
141
142     lacp->active = s->active;
143     lacp->fast = s->fast;
144 }
145
146 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
147  * configured for passive mode. */
148 bool
149 lacp_is_active(const struct lacp *lacp)
150 {
151     return lacp->active;
152 }
153
154 /* Processes 'pdu', a parsed LACP packet received on 'slave_'.  This function
155  * should be called on all packets received on 'slave_' with Ethernet Type
156  * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
157 void
158 lacp_process_pdu(struct lacp *lacp, const void *slave_,
159                  const struct lacp_pdu *pdu)
160 {
161     struct slave *slave = slave_lookup(lacp, slave_);
162
163     slave->status = LACP_CURRENT;
164     timer_set_duration(&slave->rx, (lacp->fast
165                                     ? LACP_FAST_TIME_RX
166                                     : LACP_SLOW_TIME_RX));
167
168     slave->ntt_actor = pdu->partner;
169
170     /* Update our information about our partner if it's out of date.  This may
171      * cause priorities to change so re-calculate attached status of all
172      * slaves.  */
173     if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
174         lacp->update = true;
175         slave->partner = pdu->actor;
176     }
177 }
178
179 /* Returns true if 'lacp' has successfully negotiated with its partner.  False
180  * if 'lacp' is NULL. */
181 bool
182 lacp_negotiated(const struct lacp *lacp)
183 {
184     return lacp ? lacp->negotiated : false;
185 }
186
187 /* Registers 'slave_' as subordinate to 'lacp'.  This should be called at least
188  * once per slave in a LACP managed bond.  Should also be called whenever a
189  * slave's settings change. */
190 void
191 lacp_slave_register(struct lacp *lacp, void *slave_,
192                     const struct lacp_slave_settings *s)
193 {
194     struct slave *slave = slave_lookup(lacp, slave_);
195
196     if (!slave) {
197         slave = xzalloc(sizeof *slave);
198         slave->lacp = lacp;
199         slave->aux = slave_;
200         hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
201         slave_set_defaulted(slave);
202
203         if (!lacp->key_slave) {
204             lacp->key_slave = slave;
205         }
206     }
207
208     if (!slave->name || strcmp(s->name, slave->name)) {
209         free(slave->name);
210         slave->name = xstrdup(s->name);
211     }
212
213     if (slave->port_id != s->id || slave->port_priority != s->priority) {
214         slave->port_id = s->id;
215         slave->port_priority = s->priority;
216
217         lacp->update = true;
218
219         if (lacp->active || lacp->negotiated) {
220             slave_set_expired(slave);
221         }
222     }
223 }
224
225 /* Unregisters 'slave_' with 'lacp'.  */
226 void
227 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
228 {
229     struct slave *slave = slave_lookup(lacp, slave_);
230
231     if (slave) {
232         slave_destroy(slave);
233         lacp->update = true;
234     }
235 }
236
237 /* This function should be called whenever the carrier status of 'slave_' has
238  * changed. */
239 void
240 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
241 {
242     struct slave *slave = slave_lookup(lacp, slave_);
243
244     if (slave->status == LACP_CURRENT || slave->lacp->active) {
245         slave_set_expired(slave);
246     }
247 }
248
249 /* This function should be called before enabling 'slave_' to send or receive
250  * traffic.  If it returns false, 'slave_' should not enabled.  As a
251  * convenience, returns true if 'lacp' is NULL. */
252 bool
253 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
254 {
255     if (lacp) {
256         struct slave *slave = slave_lookup(lacp, slave_);
257
258         /* The slave may be enabled if it's attached to an aggregator and its
259          * partner is synchronized.  The only exception is defaulted slaves.
260          * They are not required to have synchronized partners because they
261          * have no partners at all.  They will only be attached if negotiations
262          * failed on all slaves in the bond. */
263         return slave->attached && (slave->partner.state & LACP_STATE_SYNC
264                                    || slave->status == LACP_DEFAULTED);
265     } else {
266         return true;
267     }
268 }
269
270 /* Returns the port ID used for 'slave_' in LACP communications. */
271 uint16_t
272 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
273 {
274     struct slave *slave = slave_lookup(lacp, slave_);
275     return slave->port_id;
276 }
277
278 /* Returns true if partner information on 'slave_' is up to date.  'slave_'
279  * not being current, generally indicates a connectivity problem, or a
280  * misconfigured (or broken) partner. */
281 bool
282 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
283 {
284     return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
285 }
286
287 /* This function should be called periodically to update 'lacp'. */
288 void
289 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
290 {
291     struct slave *slave;
292
293     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
294         if (timer_expired(&slave->rx)) {
295             if (slave->status == LACP_CURRENT) {
296                 slave_set_expired(slave);
297             } else if (slave->status == LACP_EXPIRED) {
298                 slave_set_defaulted(slave);
299             }
300         }
301     }
302
303     if (lacp->update) {
304         lacp_update_attached(lacp);
305     }
306
307     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
308         struct lacp_pdu pdu;
309         struct lacp_info actor;
310
311         if (!slave_may_tx(slave)) {
312             continue;
313         }
314
315         slave_get_actor(slave, &actor);
316
317         if (timer_expired(&slave->tx)
318             || !info_tx_equal(&actor, &slave->ntt_actor)) {
319
320             slave->ntt_actor = actor;
321             compose_lacp_pdu(&actor, &slave->partner, &pdu);
322             send_pdu(slave->aux, &pdu);
323
324             timer_set_duration(&slave->tx,
325                                (slave->partner.state & LACP_STATE_TIME
326                                 ? LACP_FAST_TIME_TX
327                                 : LACP_SLOW_TIME_TX));
328         }
329     }
330 }
331
332 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
333 void
334 lacp_wait(struct lacp *lacp)
335 {
336     struct slave *slave;
337
338     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
339         if (slave_may_tx(slave)) {
340             timer_wait(&slave->tx);
341         }
342
343         if (slave->status != LACP_DEFAULTED) {
344             timer_wait(&slave->rx);
345         }
346     }
347 }
348 \f
349 /* Static Helpers. */
350
351 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
352  * negotiated parameter to true if any slaves are attachable. */
353 static void
354 lacp_update_attached(struct lacp *lacp)
355 {
356     struct slave *lead, *slave;
357     struct lacp_info lead_pri;
358     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
359
360     lacp->update = false;
361
362     lead = NULL;
363     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
364         struct lacp_info pri;
365
366         slave->attached = true;
367
368         /* XXX: In the future allow users to configure the expected system ID.
369          * For now just special case loopback. */
370         if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
371             VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
372                          "connected to its own bond", slave->name);
373             slave->attached = false;
374             continue;
375         }
376
377         if (slave->status == LACP_DEFAULTED) {
378             continue;
379         }
380
381         slave_get_priority(slave, &pri);
382
383         if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
384             lead = slave;
385             lead_pri = pri;
386         }
387     }
388
389     lacp->negotiated = lead != NULL;
390
391     if (lead) {
392         HMAP_FOR_EACH (slave, node, &lacp->slaves) {
393             if (slave->status == LACP_DEFAULTED
394                 || lead->partner.key != slave->partner.key
395                 || !eth_addr_equals(lead->partner.sys_id,
396                                     slave->partner.sys_id)) {
397                 slave->attached = false;
398             }
399         }
400     }
401 }
402
403 static void
404 slave_destroy(struct slave *slave)
405 {
406     if (slave) {
407         struct lacp *lacp = slave->lacp;
408
409         lacp->update = true;
410         hmap_remove(&lacp->slaves, &slave->node);
411
412         if (lacp->key_slave == slave) {
413             struct hmap_node *slave_node = hmap_first(&lacp->slaves);
414
415             if (slave_node) {
416                 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
417             } else {
418                 lacp->key_slave = NULL;
419             }
420         }
421
422         free(slave->name);
423         free(slave);
424     }
425 }
426
427 static void
428 slave_set_defaulted(struct slave *slave)
429 {
430     memset(&slave->partner, 0, sizeof slave->partner);
431
432     slave->lacp->update = true;
433     slave->status = LACP_DEFAULTED;
434 }
435
436 static void
437 slave_set_expired(struct slave *slave)
438 {
439     slave->status = LACP_EXPIRED;
440     slave->partner.state |= LACP_STATE_TIME;
441     slave->partner.state &= ~LACP_STATE_SYNC;
442     timer_set_duration(&slave->rx, LACP_FAST_TIME_RX);
443 }
444
445 static void
446 slave_get_actor(struct slave *slave, struct lacp_info *actor)
447 {
448     uint8_t state = 0;
449
450     if (slave->lacp->active) {
451         state |= LACP_STATE_ACT;
452     }
453
454     if (slave->lacp->fast) {
455         state |= LACP_STATE_TIME;
456     }
457
458     if (slave->attached) {
459         state |= LACP_STATE_SYNC;
460     }
461
462     if (slave->status == LACP_DEFAULTED) {
463         state |= LACP_STATE_DEF;
464     }
465
466     if (slave->status == LACP_EXPIRED) {
467         state |= LACP_STATE_EXP;
468     }
469
470     if (hmap_count(&slave->lacp->slaves) > 1) {
471         state |= LACP_STATE_AGG;
472     }
473
474     if (slave->attached || !slave->lacp->negotiated) {
475         state |= LACP_STATE_COL | LACP_STATE_DIST;
476     }
477
478     actor->state = state;
479     actor->key = htons(slave->lacp->key_slave->port_id);
480     actor->port_priority = htons(slave->port_priority);
481     actor->port_id = htons(slave->port_id);
482     actor->sys_priority = htons(slave->lacp->sys_priority);
483     memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
484 }
485
486 /* Given 'slave', populates 'priority' with data representing its LACP link
487  * priority.  If two priority objects populated by this function are compared
488  * using memcmp, the higher priority link will be less than the lower priority
489  * link. */
490 static void
491 slave_get_priority(struct slave *slave, struct lacp_info *priority)
492 {
493     uint16_t partner_priority, actor_priority;
494
495     /* Choose the lacp_info of the higher priority system by comparing their
496      * system priorities and mac addresses. */
497     actor_priority = slave->lacp->sys_priority;
498     partner_priority = ntohs(slave->partner.sys_priority);
499     if (actor_priority < partner_priority) {
500         slave_get_actor(slave, priority);
501     } else if (partner_priority < actor_priority) {
502         *priority = slave->partner;
503     } else if (eth_addr_compare_3way(slave->lacp->sys_id,
504                                      slave->partner.sys_id) < 0) {
505         slave_get_actor(slave, priority);
506     } else {
507         *priority = slave->partner;
508     }
509
510     /* Key and state are not used in priority comparisons. */
511     priority->key = 0;
512     priority->state = 0;
513 }
514
515 static bool
516 slave_may_tx(const struct slave *slave)
517 {
518     return slave->lacp->active || slave->status != LACP_DEFAULTED;
519 }
520
521 static struct slave *
522 slave_lookup(const struct lacp *lacp, const void *slave_)
523 {
524     struct slave *slave;
525
526     HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
527                              &lacp->slaves) {
528         if (slave->aux == slave_) {
529             return slave;
530         }
531     }
532
533     return NULL;
534 }
535
536 /* Two lacp_info structures are tx_equal if and only if they do not differ in
537  * ways which would require a lacp_pdu transmission. */
538 static bool
539 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
540 {
541
542     /* LACP specification dictates that we transmit whenever the actor and
543      * remote_actor differ in the following fields: Port, Port Priority,
544      * System, System Priority, Aggregation Key, Activity State, Timeout State,
545      * Sync State, and Aggregation State. The state flags are most likely to
546      * change so are checked first. */
547     return !((a->state ^ b->state) & (LACP_STATE_ACT
548                                       | LACP_STATE_TIME
549                                       | LACP_STATE_SYNC
550                                       | LACP_STATE_AGG))
551         && a->port_id == b->port_id
552         && a->port_priority == b->port_priority
553         && a->key == b->key
554         && a->sys_priority == b->sys_priority
555         && eth_addr_equals(a->sys_id, b->sys_id);
556 }
557 \f
558 static struct lacp *
559 lacp_find(const char *name)
560 {
561     struct lacp *lacp;
562
563     LIST_FOR_EACH (lacp, node, &all_lacps) {
564         if (!strcmp(lacp->name, name)) {
565             return lacp;
566         }
567     }
568
569     return NULL;
570 }
571
572 static void
573 ds_put_lacp_state(struct ds *ds, uint8_t state)
574 {
575     if (state & LACP_STATE_ACT) {
576         ds_put_cstr(ds, "activity ");
577     }
578
579     if (state & LACP_STATE_TIME) {
580         ds_put_cstr(ds, "timeout ");
581     }
582
583     if (state & LACP_STATE_AGG) {
584         ds_put_cstr(ds, "aggregation ");
585     }
586
587     if (state & LACP_STATE_SYNC) {
588         ds_put_cstr(ds, "synchronized ");
589     }
590
591     if (state & LACP_STATE_COL) {
592         ds_put_cstr(ds, "collecting ");
593     }
594
595     if (state & LACP_STATE_DIST) {
596         ds_put_cstr(ds, "distributing ");
597     }
598
599     if (state & LACP_STATE_DEF) {
600         ds_put_cstr(ds, "defaulted ");
601     }
602
603     if (state & LACP_STATE_EXP) {
604         ds_put_cstr(ds, "expired ");
605     }
606 }
607
608 static void
609 lacp_unixctl_show(struct unixctl_conn *conn,
610                   const char *args, void *aux OVS_UNUSED)
611 {
612     struct ds ds = DS_EMPTY_INITIALIZER;
613     struct lacp *lacp;
614     struct slave *slave;
615
616     lacp = lacp_find(args);
617     if (!lacp) {
618         unixctl_command_reply(conn, 501, "no such lacp object");
619         return;
620     }
621
622     ds_put_format(&ds, "lacp: %s\n", lacp->name);
623     ds_put_format(&ds, "\tstatus: %s %s\n",
624                   lacp->active ? "active" : "passive",
625                   lacp->negotiated ? "negotiated" : "");
626     ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
627     ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
628     ds_put_cstr(&ds, "\taggregation key: ");
629     if (lacp->key_slave) {
630         ds_put_format(&ds, "%u", lacp->key_slave->port_id);
631     } else {
632         ds_put_cstr(&ds, "none");
633     }
634     ds_put_cstr(&ds, "\n");
635
636     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
637         char *status;
638         struct lacp_info actor;
639
640         slave_get_actor(slave, &actor);
641         switch (slave->status) {
642         case LACP_CURRENT:
643             status = "current";
644             break;
645         case LACP_EXPIRED:
646             status = "expired";
647             break;
648         case LACP_DEFAULTED:
649             status = "defaulted";
650             break;
651         default:
652             NOT_REACHED();
653         }
654
655         ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
656                       slave->attached ? "attached" : "detached");
657         ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
658         ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
659
660         ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
661                       ETH_ADDR_ARGS(actor.sys_id));
662         ds_put_format(&ds, "\tactor sys_priority: %u\n",
663                       ntohs(actor.sys_priority));
664         ds_put_format(&ds, "\tactor port_id: %u\n",
665                       ntohs(actor.port_id));
666         ds_put_format(&ds, "\tactor port_priority: %u\n",
667                       ntohs(actor.port_priority));
668         ds_put_format(&ds, "\tactor key: %u\n",
669                       ntohs(actor.key));
670         ds_put_cstr(&ds, "\tactor state: ");
671         ds_put_lacp_state(&ds, actor.state);
672         ds_put_cstr(&ds, "\n\n");
673
674         ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
675                       ETH_ADDR_ARGS(slave->partner.sys_id));
676         ds_put_format(&ds, "\tpartner sys_priority: %u\n",
677                       ntohs(slave->partner.sys_priority));
678         ds_put_format(&ds, "\tpartner port_id: %u\n",
679                       ntohs(slave->partner.port_id));
680         ds_put_format(&ds, "\tpartner port_priority: %u\n",
681                       ntohs(slave->partner.port_priority));
682         ds_put_format(&ds, "\tpartner key: %u\n",
683                       ntohs(slave->partner.key));
684         ds_put_cstr(&ds, "\tpartner state: ");
685         ds_put_lacp_state(&ds, slave->partner.state);
686         ds_put_cstr(&ds, "\n");
687     }
688
689     unixctl_command_reply(conn, 200, ds_cstr(&ds));
690     ds_destroy(&ds);
691 }