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