8371feceb7641e42698a55a97993bda998fc781f
[sliver-openvswitch.git] / lib / lacp.h
1 /*
2  * Copyright (c) 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef LACP_H
18 #define LACP_H 1
19
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include "packets.h"
23
24 /* Masks for lacp_info state member. */
25 #define LACP_STATE_ACT  0x01 /* Activity. Active or passive? */
26 #define LACP_STATE_TIME 0x02 /* Timeout. Short or long timeout? */
27 #define LACP_STATE_AGG  0x04 /* Aggregation. Is the link is bondable? */
28 #define LACP_STATE_SYNC 0x08 /* Synchronization. Is the link in up to date? */
29 #define LACP_STATE_COL  0x10 /* Collecting. Is the link receiving frames? */
30 #define LACP_STATE_DIST 0x20 /* Distributing. Is the link sending frames? */
31 #define LACP_STATE_DEF  0x40 /* Defaulted. Using default partner info? */
32 #define LACP_STATE_EXP  0x80 /* Expired. Using expired partner info? */
33
34 #define LACP_FAST_TIME_TX 1000  /* Fast transmission rate. */
35 #define LACP_SLOW_TIME_TX 30000 /* Slow transmission rate. */
36 #define LACP_FAST_TIME_RX (LACP_FAST_TIME_TX * 3) /* Fast receive rate. */
37 #define LACP_SLOW_TIME_RX (LACP_SLOW_TIME_TX * 3) /* Slow receive rate. */
38
39 #define LACP_INFO_LEN 15
40 struct lacp_info {
41     ovs_be16 sys_priority;            /* System priority. */
42     uint8_t sys_id[ETH_ADDR_LEN];     /* System ID. */
43     ovs_be16 key;                     /* Operational key. */
44     ovs_be16 port_priority;           /* Port priority. */
45     ovs_be16 port_id;                 /* Port ID. */
46     uint8_t state;                    /* State mask.  See LACP_STATE macros. */
47 } __attribute__((packed));
48 BUILD_ASSERT_DECL(LACP_INFO_LEN == sizeof(struct lacp_info));
49
50 #define LACP_PDU_LEN 110
51 struct lacp_pdu {
52     uint8_t subtype;          /* Always 1. */
53     uint8_t version;          /* Always 1. */
54
55     uint8_t actor_type;       /* Always 1. */
56     uint8_t actor_len;        /* Always 20. */
57     struct lacp_info actor;   /* LACP actor information. */
58     uint8_t z1[3];            /* Reserved.  Always 0. */
59
60     uint8_t partner_type;     /* Always 2. */
61     uint8_t partner_len;      /* Always 20. */
62     struct lacp_info partner; /* LACP partner information. */
63     uint8_t z2[3];            /* Reserved.  Always 0. */
64
65     uint8_t collector_type;   /* Always 3. */
66     uint8_t collector_len;    /* Always 16. */
67     ovs_be16 collector_delay; /* Maximum collector delay. Set to UINT16_MAX. */
68     uint8_t z3[64];           /* Combination of several fields.  Always 0. */
69 } __attribute__((packed));
70 BUILD_ASSERT_DECL(LACP_PDU_LEN == sizeof(struct lacp_pdu));
71
72 void compose_lacp_pdu(const struct lacp_info *actor,
73                       const struct lacp_info *partner, struct lacp_pdu *);
74
75 const struct lacp_pdu *parse_lacp_packet(const struct ofpbuf *);
76 \f
77 /* LACP Protocol Implementation. */
78
79 struct lacp_settings {
80     char *name;
81     uint8_t id[ETH_ADDR_LEN];
82     uint16_t priority;
83     bool active;
84     bool fast;
85     bool strict;
86 };
87
88 void lacp_init(void);
89 struct lacp *lacp_create(void);
90 void lacp_destroy(struct lacp *);
91
92 void lacp_configure(struct lacp *, const struct lacp_settings *);
93 bool lacp_is_active(const struct lacp *);
94
95 void lacp_process_pdu(struct lacp *, const void *slave,
96                       const struct lacp_pdu *);
97 bool lacp_negotiated(const struct lacp *);
98
99 struct lacp_slave_settings {
100     char *name;
101     uint16_t id;
102     uint16_t priority;
103 };
104
105 void lacp_slave_register(struct lacp *, void *slave_,
106                          const struct lacp_slave_settings *);
107 void lacp_slave_unregister(struct lacp *, const void *slave);
108 void lacp_slave_carrier_changed(const struct lacp *, const void *slave);
109 bool lacp_slave_may_enable(const struct lacp *, const void *slave);
110 uint16_t lacp_slave_get_port_id(const struct lacp *, const void *slave);
111 bool lacp_slave_is_current(const struct lacp *, const void *slave_);
112
113 /* Callback function for lacp_run() for sending a LACP PDU. */
114 typedef void lacp_send_pdu(void *slave, const struct lacp_pdu *);
115
116 void lacp_run(struct lacp *, lacp_send_pdu *);
117 void lacp_wait(struct lacp *);
118
119 #endif /* lacp.h */