blob: c57371a27c9f7384e134c3423a558efdae392f66 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
Rich Laned983aa52013-06-13 11:48:37 -070028:: include('_copyright.c')
Rich Lanea407b912013-12-15 11:53:35 -080029:: import c_gen.of_g_legacy as of_g
30:: flow_mod = of_g.base_length[("of_flow_modify",of_g.VERSION_1_2)]
31:: packet_in = of_g.base_length[("of_packet_in",of_g.VERSION_1_2)]
32:: packet_in_1_3 = of_g.base_length[("of_packet_in",of_g.VERSION_1_3)]
33:: flow_stats = of_g.base_length[("of_flow_stats_entry", of_g.VERSION_1_2)]
34:: match1 = of_g.base_length[("of_match_v1",of_g.VERSION_1_0)]
35:: match2 = of_g.base_length[("of_match_v2",of_g.VERSION_1_1)]
Rich Lanea06d0c32013-03-25 08:52:03 -070036
37/******************************************************************************
38 *
39 * /module/src/loci_int.h
40 *
41 * loci Internal Header
42 *
43 *****************************************************************************/
44#ifndef __LOCI_INT_H__
45#define __LOCI_INT_H__
46
Rich Lanec3662bb2013-12-15 15:14:13 -080047#include <loci/loci.h>
48
Rich Laneddcbca72014-03-24 09:17:44 -070049#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
Rich Lane22811f52013-12-15 15:28:03 -080050#define UNREACHABLE() __builtin_unreachable()
51#else
52#define UNREACHABLE()
53#endif
54
Rich Lanea407b912013-12-15 11:53:35 -080055/****************************************************************
56 * Special case macros for calculating variable lengths and offsets
57 ****************************************************************/
Rich Lanea06d0c32013-03-25 08:52:03 -070058
Rich Lanea407b912013-12-15 11:53:35 -080059/**
60 * Get a u16 directly from an offset in an object's wire buffer
61 * @param obj An of_object_t object
62 * @param offset Base offset of the uint16 relative to the object
63 *
64 */
65
66static inline int
67of_object_u16_get(of_object_t *obj, int offset) {
68 uint16_t val16;
69
70 of_wire_buffer_u16_get(obj->wire_object.wbuf,
71 obj->wire_object.obj_offset + offset, &val16);
72
73 return (int)val16;
74}
75
76/**
77 * Set a u16 directly at an offset in an object's wire buffer
78 * @param obj An of_object_t object
79 * @param offset Base offset of the uint16 relative to the object
80 * @param val The value to store
81 *
82 */
83
84static inline void
85of_object_u16_set(of_object_t *obj, int offset, int value) {
86 uint16_t val16;
87
88 val16 = (uint16_t)value;
89 of_wire_buffer_u16_set(obj->wire_object.wbuf,
90 obj->wire_object.obj_offset + offset, val16);
91}
92
93/**
94 * Get length of an object with a TLV header with uint16_t
95 * @param obj An object with a match member
96 * @param offset The wire offset of the start of the object
97 *
98 * The length field follows the type field.
99 */
100
101#define _TLV16_LEN(obj, offset) \
102 (of_object_u16_get((of_object_t *)(obj), (offset) + 2))
103
104/**
105 * Get length of an object that is the "rest" of the object
106 * @param obj An object with a match member
107 * @param offset The wire offset of the start of the object
108 *
109 */
110
111#define _END_LEN(obj, offset) ((obj)->length - (offset))
112
113/**
114 * Offset of the action_len member in a packet-out object
115 */
116
117#define _PACKET_OUT_ACTION_LEN_OFFSET(obj) \
118 (((obj)->version == OF_VERSION_1_0) ? 14 : 16)
119
120/**
121 * Get length of the action list object in a packet_out object
122 * @param obj An object of type of_packet_out
123 */
124
125#define _PACKET_OUT_ACTION_LEN(obj) \
126 (of_object_u16_get((of_object_t *)(obj), _PACKET_OUT_ACTION_LEN_OFFSET(obj)))
127
128/**
129 * Set length of the action list object in a packet_out object
130 * @param obj An object of type of_packet_out
131 */
132
133#define _PACKET_OUT_ACTION_LEN_SET(obj, len) \
134 (of_object_u16_set((of_object_t *)(obj), _PACKET_OUT_ACTION_LEN_OFFSET(obj), len))
135
136/*
137 * Match structs in 1.2 come at the end of the fixed length part
138 * of structures. They add 8 bytes to the minimal length of the
139 * message, but are also variable length. This means that the
140 * type/length offsets are 8 bytes back from the end of the fixed
141 * length part of the object. The right way to handle this is to
142 * expose the offset of the match member more explicitly. For now,
143 * we make the calculation as described here.
144 */
145
146/* 1.2 min length of match is 8 bytes */
147#define _MATCH_MIN_LENGTH_V3 8
148
149/**
150 * The offset of a 1.2 match object relative to fixed length of obj
151 */
152#define _MATCH_OFFSET_V3(fixed_obj_len) \
153 ((fixed_obj_len) - _MATCH_MIN_LENGTH_V3)
154
155/**
156 * The "extra" length beyond the minimal 8 bytes of a match struct
157 * in an object
158 */
159#define _MATCH_EXTRA_LENGTH_V3(obj, fixed_obj_len) \
160 (OF_MATCH_BYTES(_TLV16_LEN(obj, _MATCH_OFFSET_V3(fixed_obj_len))) - \
161 _MATCH_MIN_LENGTH_V3)
162
163/**
164 * The offset of an object following a match object for 1.2
165 */
166#define _OFFSET_FOLLOWING_MATCH_V3(obj, fixed_obj_len) \
167 ((fixed_obj_len) + _MATCH_EXTRA_LENGTH_V3(obj, fixed_obj_len))
168
169/**
170 * Get length of a match object from its wire representation
171 * @param obj An object with a match member
172 * @param match_offset The wire offset of the match object.
173 *
174 * See above; for 1.2,
175 * The match length is raw bytes but the actual space it takes
176 * up is padded for alignment to 64-bits
177 */
178#define _WIRE_MATCH_LEN(obj, match_offset) \
179 (((obj)->version == OF_VERSION_1_0) ? ${match1} : \
180 (((obj)->version == OF_VERSION_1_1) ? ${match2} : \
181 _TLV16_LEN(obj, match_offset)))
182
183#define _WIRE_LEN_MIN 4
184
185/*
186 * Wrapper function for match len. There are cases where the wire buffer
187 * has not been set with the proper minimum length. In this case, the
188 * wire match len is interpretted as its minimum length, 4 bytes.
189 */
190
191static inline int
192wire_match_len(of_object_t *obj, int match_offset) {
193 int len;
194
195 len = _WIRE_MATCH_LEN(obj, match_offset);
196
197 return (len == 0) ? _WIRE_LEN_MIN : len;
198}
199
200#define _WIRE_MATCH_PADDED_LEN(obj, match_offset) \
201 OF_MATCH_BYTES(wire_match_len((of_object_t *)(obj), (match_offset)))
202
203/**
204 * Macro to calculate variable offset of instructions member in flow mod
205 * @param obj An object of some type of flow modify/add/delete
206 *
207 * Get length of preceding match object and add to fixed length
208 * Applies only to version 1.2
209 */
210
211#define _FLOW_MOD_INSTRUCTIONS_OFFSET(obj) \
212 _OFFSET_FOLLOWING_MATCH_V3(obj, ${flow_mod})
213
214/* The different flavors of flow mod all use the above */
215#define _FLOW_ADD_INSTRUCTIONS_OFFSET(obj) \
216 _FLOW_MOD_INSTRUCTIONS_OFFSET(obj)
217#define _FLOW_MODIFY_INSTRUCTIONS_OFFSET(obj) \
218 _FLOW_MOD_INSTRUCTIONS_OFFSET(obj)
219#define _FLOW_MODIFY_STRICT_INSTRUCTIONS_OFFSET(obj) \
220 _FLOW_MOD_INSTRUCTIONS_OFFSET(obj)
221#define _FLOW_DELETE_INSTRUCTIONS_OFFSET(obj) \
222 _FLOW_MOD_INSTRUCTIONS_OFFSET(obj)
223#define _FLOW_DELETE_STRICT_INSTRUCTIONS_OFFSET(obj) \
224 _FLOW_MOD_INSTRUCTIONS_OFFSET(obj)
225
226/**
227 * Macro to calculate variable offset of instructions member in flow stats
228 * @param obj An object of type of_flow_mod_t
229 *
230 * Get length of preceding match object and add to fixed length
231 * Applies only to version 1.2 and 1.3
232 */
233
234#define _FLOW_STATS_ENTRY_INSTRUCTIONS_OFFSET(obj) \
235 _OFFSET_FOLLOWING_MATCH_V3(obj, ${flow_stats})
236
237/**
238 * Macro to calculate variable offset of data (packet) member in packet_in
239 * @param obj An object of type of_packet_in_t
240 *
241 * Get length of preceding match object and add to fixed length
242 * Applies only to version 1.2 and 1.3
243 * The +2 comes from the 2 bytes of padding between the match and packet data.
244 */
245
246#define _PACKET_IN_DATA_OFFSET(obj) \
247 (_OFFSET_FOLLOWING_MATCH_V3((obj), (obj)->version == OF_VERSION_1_2 ? \
248${packet_in} : ${packet_in_1_3}) + 2)
249
250/**
251 * Macro to calculate variable offset of data (packet) member in packet_out
252 * @param obj An object of type of_packet_out_t
253 *
254 * Find the length in the actions_len variable and add to the fixed len
255 * Applies only to version 1.2 and 1.3
256 */
257
258#define _PACKET_OUT_DATA_OFFSET(obj) (_PACKET_OUT_ACTION_LEN(obj) + \
259 of_object_fixed_len[(obj)->version][OF_PACKET_OUT])
260
261/**
262 * Macro to map port numbers that changed across versions
263 * @param port The port_no_t variable holding the value
264 * @param ver The OpenFlow version from which the value was extracted
265 */
266#define OF_PORT_NO_VALUE_CHECK(port, ver) \
267 if (((ver) == OF_VERSION_1_0) && ((port) > 0xff00)) (port) += 0xffff0000
Rich Lanea06d0c32013-03-25 08:52:03 -0700268
Rich Lane940b8752013-12-15 11:56:50 -0800269/**
270 * Macro to detect if an object ID falls in the "flow mod" family of objects
271 * This includes add, modify, modify_strict, delete and delete_strict
272 */
273#define IS_FLOW_MOD_SUBTYPE(object_id) \
274 (((object_id) == OF_FLOW_MODIFY) || \
275 ((object_id) == OF_FLOW_MODIFY_STRICT) || \
276 ((object_id) == OF_FLOW_DELETE) || \
277 ((object_id) == OF_FLOW_DELETE_STRICT) || \
278 ((object_id) == OF_FLOW_ADD))
Rich Lanea06d0c32013-03-25 08:52:03 -0700279
Rich Lane713d9282013-12-30 15:21:35 -0800280/**
281 * Macro to calculate variable offset of value member in of_bsn_gentable_entry_add
282 * @param obj An object of type of_bsn_gentable_entry_add_t
283 */
284
285#define _BSN_GENTABLE_ENTRY_ADD_VALUE_OFFSET(obj) \
286 (of_object_u16_get(obj, 18) + \
287 of_object_fixed_len[(obj)->version][OF_BSN_GENTABLE_ENTRY_ADD])
288
289#define _BSN_GENTABLE_ENTRY_DESC_STATS_ENTRY_VALUE_OFFSET(obj) \
290 (of_object_u16_get(obj, 2) + \
291 of_object_fixed_len[(obj)->version][OF_BSN_GENTABLE_ENTRY_DESC_STATS_ENTRY])
292
293#define _BSN_GENTABLE_ENTRY_STATS_ENTRY_STATS_OFFSET(obj) \
294 (of_object_u16_get(obj, 2) + \
295 of_object_fixed_len[(obj)->version][OF_BSN_GENTABLE_ENTRY_STATS_ENTRY])
296
Rich Lanea06d0c32013-03-25 08:52:03 -0700297#endif /* __LOCI_INT_H__ */