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