summaryrefslogtreecommitdiffstats
path: root/include/osx/lacp.h
blob: 04c81c167feac888cfcd15544e438c65ed32eebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 * 
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

/*
 * lacp.h
 * - definitions for the Link Aggregation Control Protocol (LACP) and
 *   the Link Aggregation Marker Protocol
 */

/* 
 * Modification History
 *
 * May 14, 2004	Dieter Siegmund (dieter@apple.com)
 * - created
 */

#ifndef _NET_LACP_H_
#define	_NET_LACP_H_

#include <sys/types.h>
#include <string.h>

/**
 ** Link Aggregation Control Protocol (LACP) definitions
 **/
#define LACPDU_VERSION_1		1

#define LACPDU_TLV_TYPE_TERMINATOR	0x00
#define LACPDU_TLV_TYPE_ACTOR		0x01
#define LACPDU_TLV_TYPE_PARTNER	0x02
#define LACPDU_TLV_TYPE_COLLECTOR	0x03

#define LACPDU_ACTOR_TLV_LENGTH	20
#define LACPDU_PARTNER_TLV_LENGTH	20
#define LACPDU_COLLECTOR_TLV_LENGTH	16

typedef u_char lacp_actor_partner_state;
typedef u_int16_t lacp_key;
typedef u_int16_t lacp_system_priority, lacp_port_priority, lacp_port;
typedef u_int16_t lacp_collector_max_delay;
typedef struct {
    u_char 	system_id[6];
} lacp_system, *lacp_system_ref;

/*
 * LACP Actor/Partner TLV 
 */
typedef struct lacp_actor_partner_tlv_s {
    u_char	lap_tlv_type;		/* 0x01 or 0x02 */
    u_char	lap_length;		/* 20 */
    u_char	lap_system_priority[2];
    u_char	lap_system[6];
    u_char	lap_key[2];
    u_char	lap_port_priority[2];
    u_char	lap_port[2];
    u_char	lap_state;
    u_char	lap_reserved[3];
} lacp_actor_partner_tlv, *lacp_actor_partner_tlv_ref;

/*
 * LACP Collector TLV
 */
typedef struct lacp_collector_tlv_s {
    u_char	lac_tlv_type;		/* 0x03 */
    u_char	lac_length;		/* 16 */
    u_char	lac_max_delay[2];
    u_char	lac_reserved[12];
} lacp_collector_tlv, *lacp_collector_tlv_ref;


/* 
 * LACP Actor/Partner State bits
 */
#define LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY		0x01
#define LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT		0x02
#define LACP_ACTOR_PARTNER_STATE_AGGREGATION		0x04
#define LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION	0x08
#define LACP_ACTOR_PARTNER_STATE_COLLECTING		0x10
#define LACP_ACTOR_PARTNER_STATE_DISTRIBUTING		0x20
#define LACP_ACTOR_PARTNER_STATE_DEFAULTED		0x40
#define LACP_ACTOR_PARTNER_STATE_EXPIRED		0x80

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_active_lacp(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_passive_lacp(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY);
}

static __inline__ int
lacp_actor_partner_state_active_lacp(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_short_timeout(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_long_timeout(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT);
}

static __inline__ int
lacp_actor_partner_state_short_timeout(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_aggregatable(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_AGGREGATION);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_individual(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_AGGREGATION);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_aggregatable(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_AGGREGATION) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_in_sync(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_out_of_sync(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION);
}

static __inline__ int
lacp_actor_partner_state_in_sync(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_collecting(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_COLLECTING);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_not_collecting(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_COLLECTING);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_collecting(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_COLLECTING) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_distributing(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_DISTRIBUTING);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_not_distributing(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_DISTRIBUTING);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_distributing(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_DISTRIBUTING) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_defaulted(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_DEFAULTED);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_not_defaulted(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_DEFAULTED);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_defaulted(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_DEFAULTED) != 0);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_expired(lacp_actor_partner_state state)
{
    return (state | LACP_ACTOR_PARTNER_STATE_EXPIRED);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_set_not_expired(lacp_actor_partner_state state)
{
    return (state &= ~LACP_ACTOR_PARTNER_STATE_EXPIRED);
}

static __inline__ lacp_actor_partner_state
lacp_actor_partner_state_expired(lacp_actor_partner_state state)
{
    return ((state & LACP_ACTOR_PARTNER_STATE_EXPIRED) != 0);
}

/*
 * Function: lacp_uint16_set
 * Purpose:
 *   Set a field in a structure that's at least 16 bits to the given
 *   value, putting it into network byte order
 */
static __inline__ void
lacp_uint16_set(uint8_t * field, uint16_t value)
{
    uint16_t tmp_value = htons(value);
    memcpy((void *)field, (void *)&tmp_value, sizeof(uint16_t));
    return;
}

/*
 * Function: lacp_uint16_get
 * Purpose:
 *   Get a field in a structure that's at least 16 bits, converting
 *   to host byte order.
 */
static __inline__ uint16_t
lacp_uint16_get(const uint8_t * field)
{
    uint16_t tmp_field;
    memcpy((void *)&tmp_field, (void *)field, sizeof(uint16_t));
    return (ntohs(tmp_field));
}

/*
 * Function: lacp_uint32_set
 * Purpose:
 *   Set a field in a structure that's at least 32 bits to the given
 *   value, putting it into network byte order
 */
static __inline__ void
lacp_uint32_set(uint8_t * field, uint32_t value)
{
    uint32_t tmp_value = htonl(value);
    memcpy((void *)field, (void *)&tmp_value, sizeof(uint32_t));
    return;
}

/*
 * Function: lacp_uint32_get
 * Purpose:
 *   Get a field in a structure that's at least 32 bits, converting
 *   to host byte order.
 */
static __inline__ uint32_t
lacp_uint32_get(const uint8_t * field)
{
    uint32_t tmp_field;
    memcpy((void *)&tmp_field, (void *)field, sizeof(uint32_t));
    return (ntohl(tmp_field));
}

/*
 * LACP Actor/Partner TLV access functions
 */
static __inline__ void
lacp_actor_partner_tlv_set_system_priority(lacp_actor_partner_tlv_ref tlv, 
					   lacp_system_priority system_priority)
{
    lacp_uint16_set(tlv->lap_system_priority, system_priority);
    return;
}

static __inline__ lacp_system_priority
lacp_actor_partner_tlv_get_system_priority(const lacp_actor_partner_tlv_ref tlv)
{
    return (lacp_system_priority)lacp_uint16_get(tlv->lap_system_priority);
}

static __inline__ void
lacp_actor_partner_tlv_set_key(lacp_actor_partner_tlv_ref tlv, lacp_key key)
{
    lacp_uint16_set(tlv->lap_key, key);
    return;
}

static __inline__ lacp_key
lacp_actor_partner_tlv_get_key(const lacp_actor_partner_tlv_ref tlv)
{
    return (lacp_key)lacp_uint16_get(tlv->lap_key);
}

static __inline__ void
lacp_actor_partner_tlv_set_port_priority(lacp_actor_partner_tlv_ref tlv, 
					 lacp_port_priority port_priority)
{
    lacp_uint16_set(tlv->lap_port_priority, port_priority);
    return;
}

static __inline__ lacp_port_priority
lacp_actor_partner_tlv_get_port_priority(const lacp_actor_partner_tlv_ref tlv)
{
    return (lacp_port_priority)lacp_uint16_get(tlv->lap_port_priority);
}

static __inline__ void
lacp_actor_partner_tlv_set_port(lacp_actor_partner_tlv_ref tlv, lacp_port port)
{
    lacp_uint16_set(tlv->lap_port, port);
    return;
}

static __inline__ lacp_port
lacp_actor_partner_tlv_get_port(const lacp_actor_partner_tlv_ref tlv)
{
    return (lacp_port)lacp_uint16_get(tlv->lap_port);
}

/*
 * LACP Collector TLV access functions
 */
static __inline__ void
lacp_collector_tlv_set_max_delay(lacp_collector_tlv_ref tlv, 
				 lacp_collector_max_delay delay)
{
    lacp_uint16_set(tlv->lac_max_delay, delay);
    return;
}

static __inline__ lacp_collector_max_delay
lacp_collector_tlv_get_max_delay(const lacp_collector_tlv_ref tlv)
{
    return (lacp_collector_max_delay)lacp_uint16_get(tlv->lac_max_delay);
}

typedef struct lacpdu_s {
    u_char		la_subtype;
    u_char		la_version;
    u_char		la_actor_tlv[LACPDU_ACTOR_TLV_LENGTH];
    u_char		la_partner_tlv[LACPDU_PARTNER_TLV_LENGTH];
    u_char		la_collector_tlv[LACPDU_COLLECTOR_TLV_LENGTH];
    u_char		la_terminator_type;
    u_char		la_terminator_length;
    u_char		la_reserved[50];
} lacpdu, *lacpdu_ref;

/* timer values in seconds */
#define LACP_FAST_PERIODIC_TIME		1 
#define LACP_SLOW_PERIODIC_TIME		30
#define LACP_SHORT_TIMEOUT_TIME		3
#define LACP_LONG_TIMEOUT_TIME		90
#define LACP_CHURN_DETECTION_TIME	60
#define LACP_AGGREGATE_WAIT_TIME	2

/* packet rate per second */
#define LACP_PACKET_RATE		3

/**
 ** Link Aggregation Marker Protocol definitions
 **/
#define LA_MARKER_PDU_VERSION_1			1
#define LA_MARKER_TLV_TYPE_TERMINATOR		0x00
#define LA_MARKER_TLV_TYPE_MARKER		0x01
#define LA_MARKER_TLV_TYPE_MARKER_RESPONSE	0x02

#define LA_MARKER_TLV_LENGTH			16
#define LA_MARKER_RESPONSE_TLV_LENGTH		16

typedef u_int32_t la_marker_transaction_id;

typedef struct la_marker_pdu_s {
    u_char		lm_subtype;		/* 0x02 */
    u_char		lm_version;		/* 0x01 */
    u_char		lm_marker_tlv_type;	/* 0x01 or 0x02 */
    u_char		lm_marker_tlv_length;	/* 16 */
    u_char		lm_requestor_port[2];
    u_char		lm_requestor_system[6];
    u_char		lm_requestor_transaction_id[4];
    u_char		lm_pad[2];
    u_char		lm_terminator_type;	/* 0x00 */
    u_char		lm_terminator_length;	/* 0 */
    u_char		lm_reserved[90];
} la_marker_pdu, *la_marker_pdu_ref, 
    la_marker_response_pdu, * la_marker_response_pdu_ref;

static __inline__ void
la_marker_pdu_set_requestor_port(la_marker_pdu_ref lmpdu, lacp_port port)
{
    lacp_uint16_set(lmpdu->lm_requestor_port, port);
    return;
}

static __inline__ lacp_port
la_marker_pdu_get_requestor_port(la_marker_pdu_ref lmpdu)
{
    return (lacp_port)lacp_uint16_get(lmpdu->lm_requestor_port);
}

static __inline__ void
la_marker_pdu_set_requestor_transaction_id(la_marker_pdu_ref lmpdu, 
					   la_marker_transaction_id xid)
{
    lacp_uint32_set(lmpdu->lm_requestor_transaction_id, xid);
    return;
}

static __inline__ la_marker_transaction_id
la_marker_pdu_get_requestor_transaction_id(la_marker_pdu_ref lmpdu)
{
    return (la_marker_transaction_id)lacp_uint32_get(lmpdu->lm_requestor_transaction_id);
}

static __inline__ void
la_marker_pdu_set_requestor_system(la_marker_pdu_ref lmpdu, lacp_system sys)
{
    *((lacp_system_ref)lmpdu->lm_requestor_system) = sys;
    return;
}

static __inline__ lacp_system
la_marker_pdu_get_requestor_system(la_marker_pdu_ref lmpdu)
{
    return (*(lacp_system_ref)(lmpdu->lm_requestor_system));
}

#endif /* _NET_LACP_H_ */