lacp: Remove enabled flag.
[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     memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
136     lacp->sys_priority = s->priority;
137     lacp->active = s->active;
138     lacp->fast = s->fast;
139 }
140
141 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
142  * configured for passive mode. */
143 bool
144 lacp_is_active(const struct lacp *lacp)
145 {
146     return lacp->active;
147 }
148
149 /* Processes 'pdu', a parsed LACP packet received on 'slave_'.  This function
150  * should be called on all packets received on 'slave_' with Ethernet Type
151  * ETH_TYPE_LACP and parsable by parse_lacp_packet(). */
152 void
153 lacp_process_pdu(struct lacp *lacp, const void *slave_,
154                  const struct lacp_pdu *pdu)
155 {
156     struct slave *slave = slave_lookup(lacp, slave_);
157
158     slave->status = LACP_CURRENT;
159     timer_set_duration(&slave->rx, (lacp->fast
160                                     ? LACP_FAST_TIME_RX
161                                     : LACP_SLOW_TIME_RX));
162
163     slave->ntt_actor = pdu->partner;
164
165     /* Update our information about our partner if it's out of date.  This may
166      * cause priorities to change so re-calculate attached status of all
167      * slaves.  */
168     if (memcmp(&slave->partner, &pdu->actor, sizeof pdu->actor)) {
169         lacp->update = true;
170         slave->partner = pdu->actor;
171     }
172 }
173
174 /* Returns true if 'lacp' has successfully negotiated with its partner.  False
175  * if 'lacp' is NULL. */
176 bool
177 lacp_negotiated(const struct lacp *lacp)
178 {
179     return lacp ? lacp->negotiated : false;
180 }
181
182 /* Registers 'slave_' as subordinate to 'lacp'.  This should be called at least
183  * once per slave in a LACP managed bond.  Should also be called whenever a
184  * slave's settings change. */
185 void
186 lacp_slave_register(struct lacp *lacp, void *slave_,
187                     const struct lacp_slave_settings *s)
188 {
189     struct slave *slave = slave_lookup(lacp, slave_);
190
191     if (!slave) {
192         slave = xzalloc(sizeof *slave);
193         slave->lacp = lacp;
194         slave->aux = slave_;
195         hmap_insert(&lacp->slaves, &slave->node, hash_pointer(slave_, 0));
196         slave_set_defaulted(slave);
197
198         if (!lacp->key_slave) {
199             lacp->key_slave = slave;
200         }
201     }
202
203     if (!slave->name || strcmp(s->name, slave->name)) {
204         free(slave->name);
205         slave->name = xstrdup(s->name);
206     }
207
208     if (slave->port_id != s->id || slave->port_priority != s->priority) {
209         slave->port_id = s->id;
210         slave->port_priority = s->priority;
211
212         lacp->update = true;
213
214         if (lacp->active || lacp->negotiated) {
215             slave_set_expired(slave);
216         }
217     }
218 }
219
220 /* Unregisters 'slave_' with 'lacp'.  */
221 void
222 lacp_slave_unregister(struct lacp *lacp, const void *slave_)
223 {
224     struct slave *slave = slave_lookup(lacp, slave_);
225
226     if (slave) {
227         slave_destroy(slave);
228     }
229 }
230
231 /* This function should be called whenever the carrier status of 'slave_' has
232  * changed. */
233 void
234 lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
235 {
236     struct slave *slave = slave_lookup(lacp, slave_);
237
238     if (slave->status == LACP_CURRENT || slave->lacp->active) {
239         slave_set_expired(slave);
240     }
241 }
242
243 /* This function should be called before enabling 'slave_' to send or receive
244  * traffic.  If it returns false, 'slave_' should not enabled.  As a
245  * convenience, returns true if 'lacp' is NULL. */
246 bool
247 lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
248 {
249     if (lacp) {
250         struct slave *slave = slave_lookup(lacp, slave_);
251
252         /* The slave may be enabled if it's attached to an aggregator and its
253          * partner is synchronized.  The only exception is defaulted slaves.
254          * They are not required to have synchronized partners because they
255          * have no partners at all.  They will only be attached if negotiations
256          * failed on all slaves in the bond. */
257         return slave->attached && (slave->partner.state & LACP_STATE_SYNC
258                                    || slave->status == LACP_DEFAULTED);
259     } else {
260         return true;
261     }
262 }
263
264 /* Returns the port ID used for 'slave_' in LACP communications. */
265 uint16_t
266 lacp_slave_get_port_id(const struct lacp *lacp, const void *slave_)
267 {
268     struct slave *slave = slave_lookup(lacp, slave_);
269     return slave->port_id;
270 }
271
272 /* This function should be called periodically to update 'lacp'. */
273 void
274 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
275 {
276     struct slave *slave;
277
278     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
279         if (timer_expired(&slave->rx)) {
280             if (slave->status == LACP_CURRENT) {
281                 slave_set_expired(slave);
282             } else if (slave->status == LACP_EXPIRED) {
283                 slave_set_defaulted(slave);
284             }
285         }
286     }
287
288     if (lacp->update) {
289         lacp_update_attached(lacp);
290     }
291
292     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
293         struct lacp_pdu pdu;
294         struct lacp_info actor;
295
296         if (!slave_may_tx(slave)) {
297             continue;
298         }
299
300         slave_get_actor(slave, &actor);
301
302         if (timer_expired(&slave->tx)
303             || !info_tx_equal(&actor, &slave->ntt_actor)) {
304
305             slave->ntt_actor = actor;
306             compose_lacp_pdu(&actor, &slave->partner, &pdu);
307             send_pdu(slave->aux, &pdu);
308
309             timer_set_duration(&slave->tx,
310                                (slave->partner.state & LACP_STATE_TIME
311                                 ? LACP_FAST_TIME_TX
312                                 : LACP_SLOW_TIME_TX));
313         }
314     }
315 }
316
317 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
318 void
319 lacp_wait(struct lacp *lacp)
320 {
321     struct slave *slave;
322
323     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
324         if (slave_may_tx(slave)) {
325             timer_wait(&slave->tx);
326         }
327
328         if (slave->status != LACP_DEFAULTED) {
329             timer_wait(&slave->rx);
330         }
331     }
332 }
333 \f
334 /* Static Helpers. */
335
336 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
337  * negotiated parameter to true if any slaves are attachable. */
338 static void
339 lacp_update_attached(struct lacp *lacp)
340 {
341     struct slave *lead, *slave;
342     struct lacp_info lead_pri;
343     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
344
345     lacp->update = false;
346
347     lead = NULL;
348     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
349         struct lacp_info pri;
350
351         slave->attached = true;
352
353         /* XXX: In the future allow users to configure the expected system ID.
354          * For now just special case loopback. */
355         if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
356             VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
357                          "connected to its own bond", slave->name);
358             slave->attached = false;
359             continue;
360         }
361
362         if (slave->status == LACP_DEFAULTED) {
363             continue;
364         }
365
366         slave_get_priority(slave, &pri);
367
368         if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
369             lead = slave;
370             lead_pri = pri;
371         }
372     }
373
374     lacp->negotiated = lead != NULL;
375
376     if (lead) {
377         HMAP_FOR_EACH (slave, node, &lacp->slaves) {
378             if (slave->status == LACP_DEFAULTED
379                 || lead->partner.key != slave->partner.key
380                 || !eth_addr_equals(lead->partner.sys_id,
381                                     slave->partner.sys_id)) {
382                 slave->attached = false;
383             }
384         }
385     }
386 }
387
388 static void
389 slave_destroy(struct slave *slave)
390 {
391     if (slave) {
392         struct lacp *lacp = slave->lacp;
393
394         lacp->update = true;
395         hmap_remove(&lacp->slaves, &slave->node);
396
397         if (lacp->key_slave == slave) {
398             struct hmap_node *slave_node = hmap_first(&lacp->slaves);
399
400             if (slave_node) {
401                 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
402             } else {
403                 lacp->key_slave = NULL;
404             }
405         }
406
407         free(slave->name);
408         free(slave);
409     }
410 }
411
412 static void
413 slave_set_defaulted(struct slave *slave)
414 {
415     memset(&slave->partner, 0, sizeof slave->partner);
416
417     slave->lacp->update = true;
418     slave->status = LACP_DEFAULTED;
419 }
420
421 static void
422 slave_set_expired(struct slave *slave)
423 {
424     slave->status = LACP_EXPIRED;
425     slave->partner.state |= LACP_STATE_TIME;
426     slave->partner.state &= ~LACP_STATE_SYNC;
427     timer_set_duration(&slave->rx, LACP_FAST_TIME_RX);
428 }
429
430 static void
431 slave_get_actor(struct slave *slave, struct lacp_info *actor)
432 {
433     uint8_t state = 0;
434
435     if (slave->lacp->active) {
436         state |= LACP_STATE_ACT;
437     }
438
439     if (slave->lacp->fast) {
440         state |= LACP_STATE_TIME;
441     }
442
443     if (slave->attached) {
444         state |= LACP_STATE_SYNC;
445     }
446
447     if (slave->status == LACP_DEFAULTED) {
448         state |= LACP_STATE_DEF;
449     }
450
451     if (slave->status == LACP_EXPIRED) {
452         state |= LACP_STATE_EXP;
453     }
454
455     if (hmap_count(&slave->lacp->slaves) > 1) {
456         state |= LACP_STATE_AGG;
457     }
458
459     if (slave->attached || !slave->lacp->negotiated) {
460         state |= LACP_STATE_COL | LACP_STATE_DIST;
461     }
462
463     actor->state = state;
464     actor->key = htons(slave->lacp->key_slave->port_id);
465     actor->port_priority = htons(slave->port_priority);
466     actor->port_id = htons(slave->port_id);
467     actor->sys_priority = htons(slave->lacp->sys_priority);
468     memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
469 }
470
471 /* Given 'slave', populates 'priority' with data representing its LACP link
472  * priority.  If two priority objects populated by this function are compared
473  * using memcmp, the higher priority link will be less than the lower priority
474  * link. */
475 static void
476 slave_get_priority(struct slave *slave, struct lacp_info *priority)
477 {
478     uint16_t partner_priority, actor_priority;
479
480     /* Choose the lacp_info of the higher priority system by comparing their
481      * system priorities and mac addresses. */
482     actor_priority = slave->lacp->sys_priority;
483     partner_priority = ntohs(slave->partner.sys_priority);
484     if (actor_priority < partner_priority) {
485         slave_get_actor(slave, priority);
486     } else if (partner_priority < actor_priority) {
487         *priority = slave->partner;
488     } else if (eth_addr_compare_3way(slave->lacp->sys_id,
489                                      slave->partner.sys_id) < 0) {
490         slave_get_actor(slave, priority);
491     } else {
492         *priority = slave->partner;
493     }
494
495     /* Key and state are not used in priority comparisons. */
496     priority->key = 0;
497     priority->state = 0;
498 }
499
500 static bool
501 slave_may_tx(const struct slave *slave)
502 {
503     return slave->lacp->active || slave->status != LACP_DEFAULTED;
504 }
505
506 static struct slave *
507 slave_lookup(const struct lacp *lacp, const void *slave_)
508 {
509     struct slave *slave;
510
511     HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
512                              &lacp->slaves) {
513         if (slave->aux == slave_) {
514             return slave;
515         }
516     }
517
518     return NULL;
519 }
520
521 /* Two lacp_info structures are tx_equal if and only if they do not differ in
522  * ways which would require a lacp_pdu transmission. */
523 static bool
524 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
525 {
526
527     /* LACP specification dictates that we transmit whenever the actor and
528      * remote_actor differ in the following fields: Port, Port Priority,
529      * System, System Priority, Aggregation Key, Activity State, Timeout State,
530      * Sync State, and Aggregation State. The state flags are most likely to
531      * change so are checked first. */
532     return !((a->state ^ b->state) & (LACP_STATE_ACT
533                                       | LACP_STATE_TIME
534                                       | LACP_STATE_SYNC
535                                       | LACP_STATE_AGG))
536         && a->port_id == b->port_id
537         && a->port_priority == b->port_priority
538         && a->key == b->key
539         && a->sys_priority == b->sys_priority
540         && eth_addr_equals(a->sys_id, b->sys_id);
541 }
542 \f
543 static struct lacp *
544 lacp_find(const char *name)
545 {
546     struct lacp *lacp;
547
548     LIST_FOR_EACH (lacp, node, &all_lacps) {
549         if (!strcmp(lacp->name, name)) {
550             return lacp;
551         }
552     }
553
554     return NULL;
555 }
556
557 static void
558 ds_put_lacp_state(struct ds *ds, uint8_t state)
559 {
560     if (state & LACP_STATE_ACT) {
561         ds_put_cstr(ds, "activity ");
562     }
563
564     if (state & LACP_STATE_TIME) {
565         ds_put_cstr(ds, "timeout ");
566     }
567
568     if (state & LACP_STATE_AGG) {
569         ds_put_cstr(ds, "aggregation ");
570     }
571
572     if (state & LACP_STATE_SYNC) {
573         ds_put_cstr(ds, "synchronized ");
574     }
575
576     if (state & LACP_STATE_COL) {
577         ds_put_cstr(ds, "collecting ");
578     }
579
580     if (state & LACP_STATE_DIST) {
581         ds_put_cstr(ds, "distributing ");
582     }
583
584     if (state & LACP_STATE_DEF) {
585         ds_put_cstr(ds, "defaulted ");
586     }
587
588     if (state & LACP_STATE_EXP) {
589         ds_put_cstr(ds, "expired ");
590     }
591 }
592
593 static void
594 lacp_unixctl_show(struct unixctl_conn *conn,
595                   const char *args, void *aux OVS_UNUSED)
596 {
597     struct ds ds = DS_EMPTY_INITIALIZER;
598     struct lacp *lacp;
599     struct slave *slave;
600
601     lacp = lacp_find(args);
602     if (!lacp) {
603         unixctl_command_reply(conn, 501, "no such lacp object");
604         return;
605     }
606
607     ds_put_format(&ds, "lacp: %s\n", lacp->name);
608     ds_put_format(&ds, "\tstatus: %s %s\n",
609                   lacp->active ? "active" : "passive",
610                   lacp->negotiated ? "negotiated" : "");
611     ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
612     ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
613     ds_put_cstr(&ds, "\taggregation key: ");
614     if (lacp->key_slave) {
615         ds_put_format(&ds, "%u", lacp->key_slave->port_id);
616     } else {
617         ds_put_cstr(&ds, "none");
618     }
619     ds_put_cstr(&ds, "\n");
620
621     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
622         char *status;
623         struct lacp_info actor;
624
625         slave_get_actor(slave, &actor);
626         switch (slave->status) {
627         case LACP_CURRENT:
628             status = "current";
629             break;
630         case LACP_EXPIRED:
631             status = "expired";
632             break;
633         case LACP_DEFAULTED:
634             status = "defaulted";
635             break;
636         default:
637             NOT_REACHED();
638         }
639
640         ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
641                       slave->attached ? "attached" : "detached");
642         ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
643         ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
644
645         ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
646                       ETH_ADDR_ARGS(actor.sys_id));
647         ds_put_format(&ds, "\tactor sys_priority: %u\n",
648                       ntohs(actor.sys_priority));
649         ds_put_format(&ds, "\tactor port_id: %u\n",
650                       ntohs(actor.port_id));
651         ds_put_format(&ds, "\tactor port_priority: %u\n",
652                       ntohs(actor.port_priority));
653         ds_put_format(&ds, "\tactor key: %u\n",
654                       ntohs(actor.key));
655         ds_put_cstr(&ds, "\tactor state: ");
656         ds_put_lacp_state(&ds, actor.state);
657         ds_put_cstr(&ds, "\n\n");
658
659         ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
660                       ETH_ADDR_ARGS(slave->partner.sys_id));
661         ds_put_format(&ds, "\tpartner sys_priority: %u\n",
662                       ntohs(slave->partner.sys_priority));
663         ds_put_format(&ds, "\tpartner port_id: %u\n",
664                       ntohs(slave->partner.port_id));
665         ds_put_format(&ds, "\tpartner port_priority: %u\n",
666                       ntohs(slave->partner.port_priority));
667         ds_put_format(&ds, "\tpartner key: %u\n",
668                       ntohs(slave->partner.key));
669         ds_put_cstr(&ds, "\tpartner state: ");
670         ds_put_lacp_state(&ds, slave->partner.state);
671         ds_put_cstr(&ds, "\n");
672     }
673
674     unixctl_command_reply(conn, 200, ds_cstr(&ds));
675     ds_destroy(&ds);
676 }