lacp: New function lacp_slave_is_current().
[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 /* Returns true if partner information on 'slave_' is up to date.  'slave_'
273  * not being current, generally indicates a connectivity problem, or a
274  * misconfigured (or broken) partner. */
275 bool
276 lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
277 {
278     return slave_lookup(lacp, slave_)->status == LACP_CURRENT;
279 }
280
281 /* This function should be called periodically to update 'lacp'. */
282 void
283 lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu)
284 {
285     struct slave *slave;
286
287     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
288         if (timer_expired(&slave->rx)) {
289             if (slave->status == LACP_CURRENT) {
290                 slave_set_expired(slave);
291             } else if (slave->status == LACP_EXPIRED) {
292                 slave_set_defaulted(slave);
293             }
294         }
295     }
296
297     if (lacp->update) {
298         lacp_update_attached(lacp);
299     }
300
301     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
302         struct lacp_pdu pdu;
303         struct lacp_info actor;
304
305         if (!slave_may_tx(slave)) {
306             continue;
307         }
308
309         slave_get_actor(slave, &actor);
310
311         if (timer_expired(&slave->tx)
312             || !info_tx_equal(&actor, &slave->ntt_actor)) {
313
314             slave->ntt_actor = actor;
315             compose_lacp_pdu(&actor, &slave->partner, &pdu);
316             send_pdu(slave->aux, &pdu);
317
318             timer_set_duration(&slave->tx,
319                                (slave->partner.state & LACP_STATE_TIME
320                                 ? LACP_FAST_TIME_TX
321                                 : LACP_SLOW_TIME_TX));
322         }
323     }
324 }
325
326 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
327 void
328 lacp_wait(struct lacp *lacp)
329 {
330     struct slave *slave;
331
332     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
333         if (slave_may_tx(slave)) {
334             timer_wait(&slave->tx);
335         }
336
337         if (slave->status != LACP_DEFAULTED) {
338             timer_wait(&slave->rx);
339         }
340     }
341 }
342 \f
343 /* Static Helpers. */
344
345 /* Updates the attached status of all slaves controlled b 'lacp' and sets its
346  * negotiated parameter to true if any slaves are attachable. */
347 static void
348 lacp_update_attached(struct lacp *lacp)
349 {
350     struct slave *lead, *slave;
351     struct lacp_info lead_pri;
352     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
353
354     lacp->update = false;
355
356     lead = NULL;
357     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
358         struct lacp_info pri;
359
360         slave->attached = true;
361
362         /* XXX: In the future allow users to configure the expected system ID.
363          * For now just special case loopback. */
364         if (eth_addr_equals(slave->partner.sys_id, slave->lacp->sys_id)) {
365             VLOG_WARN_RL(&rl, "slave %s: Loopback detected. Slave is "
366                          "connected to its own bond", slave->name);
367             slave->attached = false;
368             continue;
369         }
370
371         if (slave->status == LACP_DEFAULTED) {
372             continue;
373         }
374
375         slave_get_priority(slave, &pri);
376
377         if (!lead || memcmp(&pri, &lead_pri, sizeof pri) < 0) {
378             lead = slave;
379             lead_pri = pri;
380         }
381     }
382
383     lacp->negotiated = lead != NULL;
384
385     if (lead) {
386         HMAP_FOR_EACH (slave, node, &lacp->slaves) {
387             if (slave->status == LACP_DEFAULTED
388                 || lead->partner.key != slave->partner.key
389                 || !eth_addr_equals(lead->partner.sys_id,
390                                     slave->partner.sys_id)) {
391                 slave->attached = false;
392             }
393         }
394     }
395 }
396
397 static void
398 slave_destroy(struct slave *slave)
399 {
400     if (slave) {
401         struct lacp *lacp = slave->lacp;
402
403         lacp->update = true;
404         hmap_remove(&lacp->slaves, &slave->node);
405
406         if (lacp->key_slave == slave) {
407             struct hmap_node *slave_node = hmap_first(&lacp->slaves);
408
409             if (slave_node) {
410                 lacp->key_slave = CONTAINER_OF(slave_node, struct slave, node);
411             } else {
412                 lacp->key_slave = NULL;
413             }
414         }
415
416         free(slave->name);
417         free(slave);
418     }
419 }
420
421 static void
422 slave_set_defaulted(struct slave *slave)
423 {
424     memset(&slave->partner, 0, sizeof slave->partner);
425
426     slave->lacp->update = true;
427     slave->status = LACP_DEFAULTED;
428 }
429
430 static void
431 slave_set_expired(struct slave *slave)
432 {
433     slave->status = LACP_EXPIRED;
434     slave->partner.state |= LACP_STATE_TIME;
435     slave->partner.state &= ~LACP_STATE_SYNC;
436     timer_set_duration(&slave->rx, LACP_FAST_TIME_RX);
437 }
438
439 static void
440 slave_get_actor(struct slave *slave, struct lacp_info *actor)
441 {
442     uint8_t state = 0;
443
444     if (slave->lacp->active) {
445         state |= LACP_STATE_ACT;
446     }
447
448     if (slave->lacp->fast) {
449         state |= LACP_STATE_TIME;
450     }
451
452     if (slave->attached) {
453         state |= LACP_STATE_SYNC;
454     }
455
456     if (slave->status == LACP_DEFAULTED) {
457         state |= LACP_STATE_DEF;
458     }
459
460     if (slave->status == LACP_EXPIRED) {
461         state |= LACP_STATE_EXP;
462     }
463
464     if (hmap_count(&slave->lacp->slaves) > 1) {
465         state |= LACP_STATE_AGG;
466     }
467
468     if (slave->attached || !slave->lacp->negotiated) {
469         state |= LACP_STATE_COL | LACP_STATE_DIST;
470     }
471
472     actor->state = state;
473     actor->key = htons(slave->lacp->key_slave->port_id);
474     actor->port_priority = htons(slave->port_priority);
475     actor->port_id = htons(slave->port_id);
476     actor->sys_priority = htons(slave->lacp->sys_priority);
477     memcpy(&actor->sys_id, slave->lacp->sys_id, ETH_ADDR_LEN);
478 }
479
480 /* Given 'slave', populates 'priority' with data representing its LACP link
481  * priority.  If two priority objects populated by this function are compared
482  * using memcmp, the higher priority link will be less than the lower priority
483  * link. */
484 static void
485 slave_get_priority(struct slave *slave, struct lacp_info *priority)
486 {
487     uint16_t partner_priority, actor_priority;
488
489     /* Choose the lacp_info of the higher priority system by comparing their
490      * system priorities and mac addresses. */
491     actor_priority = slave->lacp->sys_priority;
492     partner_priority = ntohs(slave->partner.sys_priority);
493     if (actor_priority < partner_priority) {
494         slave_get_actor(slave, priority);
495     } else if (partner_priority < actor_priority) {
496         *priority = slave->partner;
497     } else if (eth_addr_compare_3way(slave->lacp->sys_id,
498                                      slave->partner.sys_id) < 0) {
499         slave_get_actor(slave, priority);
500     } else {
501         *priority = slave->partner;
502     }
503
504     /* Key and state are not used in priority comparisons. */
505     priority->key = 0;
506     priority->state = 0;
507 }
508
509 static bool
510 slave_may_tx(const struct slave *slave)
511 {
512     return slave->lacp->active || slave->status != LACP_DEFAULTED;
513 }
514
515 static struct slave *
516 slave_lookup(const struct lacp *lacp, const void *slave_)
517 {
518     struct slave *slave;
519
520     HMAP_FOR_EACH_IN_BUCKET (slave, node, hash_pointer(slave_, 0),
521                              &lacp->slaves) {
522         if (slave->aux == slave_) {
523             return slave;
524         }
525     }
526
527     return NULL;
528 }
529
530 /* Two lacp_info structures are tx_equal if and only if they do not differ in
531  * ways which would require a lacp_pdu transmission. */
532 static bool
533 info_tx_equal(struct lacp_info *a, struct lacp_info *b)
534 {
535
536     /* LACP specification dictates that we transmit whenever the actor and
537      * remote_actor differ in the following fields: Port, Port Priority,
538      * System, System Priority, Aggregation Key, Activity State, Timeout State,
539      * Sync State, and Aggregation State. The state flags are most likely to
540      * change so are checked first. */
541     return !((a->state ^ b->state) & (LACP_STATE_ACT
542                                       | LACP_STATE_TIME
543                                       | LACP_STATE_SYNC
544                                       | LACP_STATE_AGG))
545         && a->port_id == b->port_id
546         && a->port_priority == b->port_priority
547         && a->key == b->key
548         && a->sys_priority == b->sys_priority
549         && eth_addr_equals(a->sys_id, b->sys_id);
550 }
551 \f
552 static struct lacp *
553 lacp_find(const char *name)
554 {
555     struct lacp *lacp;
556
557     LIST_FOR_EACH (lacp, node, &all_lacps) {
558         if (!strcmp(lacp->name, name)) {
559             return lacp;
560         }
561     }
562
563     return NULL;
564 }
565
566 static void
567 ds_put_lacp_state(struct ds *ds, uint8_t state)
568 {
569     if (state & LACP_STATE_ACT) {
570         ds_put_cstr(ds, "activity ");
571     }
572
573     if (state & LACP_STATE_TIME) {
574         ds_put_cstr(ds, "timeout ");
575     }
576
577     if (state & LACP_STATE_AGG) {
578         ds_put_cstr(ds, "aggregation ");
579     }
580
581     if (state & LACP_STATE_SYNC) {
582         ds_put_cstr(ds, "synchronized ");
583     }
584
585     if (state & LACP_STATE_COL) {
586         ds_put_cstr(ds, "collecting ");
587     }
588
589     if (state & LACP_STATE_DIST) {
590         ds_put_cstr(ds, "distributing ");
591     }
592
593     if (state & LACP_STATE_DEF) {
594         ds_put_cstr(ds, "defaulted ");
595     }
596
597     if (state & LACP_STATE_EXP) {
598         ds_put_cstr(ds, "expired ");
599     }
600 }
601
602 static void
603 lacp_unixctl_show(struct unixctl_conn *conn,
604                   const char *args, void *aux OVS_UNUSED)
605 {
606     struct ds ds = DS_EMPTY_INITIALIZER;
607     struct lacp *lacp;
608     struct slave *slave;
609
610     lacp = lacp_find(args);
611     if (!lacp) {
612         unixctl_command_reply(conn, 501, "no such lacp object");
613         return;
614     }
615
616     ds_put_format(&ds, "lacp: %s\n", lacp->name);
617     ds_put_format(&ds, "\tstatus: %s %s\n",
618                   lacp->active ? "active" : "passive",
619                   lacp->negotiated ? "negotiated" : "");
620     ds_put_format(&ds, "\tsys_id: " ETH_ADDR_FMT "\n", ETH_ADDR_ARGS(lacp->sys_id));
621     ds_put_format(&ds, "\tsys_priority: %u\n", lacp->sys_priority);
622     ds_put_cstr(&ds, "\taggregation key: ");
623     if (lacp->key_slave) {
624         ds_put_format(&ds, "%u", lacp->key_slave->port_id);
625     } else {
626         ds_put_cstr(&ds, "none");
627     }
628     ds_put_cstr(&ds, "\n");
629
630     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
631         char *status;
632         struct lacp_info actor;
633
634         slave_get_actor(slave, &actor);
635         switch (slave->status) {
636         case LACP_CURRENT:
637             status = "current";
638             break;
639         case LACP_EXPIRED:
640             status = "expired";
641             break;
642         case LACP_DEFAULTED:
643             status = "defaulted";
644             break;
645         default:
646             NOT_REACHED();
647         }
648
649         ds_put_format(&ds, "\nslave: %s: %s %s\n", slave->name, status,
650                       slave->attached ? "attached" : "detached");
651         ds_put_format(&ds, "\tport_id: %u\n", slave->port_id);
652         ds_put_format(&ds, "\tport_priority: %u\n", slave->port_priority);
653
654         ds_put_format(&ds, "\n\tactor sys_id: " ETH_ADDR_FMT "\n",
655                       ETH_ADDR_ARGS(actor.sys_id));
656         ds_put_format(&ds, "\tactor sys_priority: %u\n",
657                       ntohs(actor.sys_priority));
658         ds_put_format(&ds, "\tactor port_id: %u\n",
659                       ntohs(actor.port_id));
660         ds_put_format(&ds, "\tactor port_priority: %u\n",
661                       ntohs(actor.port_priority));
662         ds_put_format(&ds, "\tactor key: %u\n",
663                       ntohs(actor.key));
664         ds_put_cstr(&ds, "\tactor state: ");
665         ds_put_lacp_state(&ds, actor.state);
666         ds_put_cstr(&ds, "\n\n");
667
668         ds_put_format(&ds, "\tpartner sys_id: " ETH_ADDR_FMT "\n",
669                       ETH_ADDR_ARGS(slave->partner.sys_id));
670         ds_put_format(&ds, "\tpartner sys_priority: %u\n",
671                       ntohs(slave->partner.sys_priority));
672         ds_put_format(&ds, "\tpartner port_id: %u\n",
673                       ntohs(slave->partner.port_id));
674         ds_put_format(&ds, "\tpartner port_priority: %u\n",
675                       ntohs(slave->partner.port_priority));
676         ds_put_format(&ds, "\tpartner key: %u\n",
677                       ntohs(slave->partner.key));
678         ds_put_cstr(&ds, "\tpartner state: ");
679         ds_put_lacp_state(&ds, slave->partner.state);
680         ds_put_cstr(&ds, "\n");
681     }
682
683     unixctl_command_reply(conn, 200, ds_cstr(&ds));
684     ds_destroy(&ds);
685 }