blob: 165fe516a36874d72b397db6c39d19c78f224ac6 [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 Lanea06d0c32013-03-25 08:52:03 -070029
30/*
31 * These routines manipulate a low level buffer assuming it holds
32 * an OpenFlow message.
33 */
34
35#if !defined(_OF_MESSAGE_H_)
36#define _OF_MESSAGE_H_
37
38#include <loci/of_buffer.h>
39
40typedef uint8_t *of_message_t;
41
42/* A few key common header offsets */
43#define OF_MESSAGE_VERSION_OFFSET 0
44#define OF_MESSAGE_TYPE_OFFSET 1
45#define OF_MESSAGE_LENGTH_OFFSET 2
46#define OF_MESSAGE_XID_OFFSET 4
47#define OF_MESSAGE_HEADER_LENGTH 8
Rob Vaterlausb3f49d92013-10-01 17:57:31 -070048#define OF_MESSAGE_ERROR_TYPE_OFFSET 8
Rich Lanea06d0c32013-03-25 08:52:03 -070049#define OF_MESSAGE_STATS_TYPE_OFFSET 8
50#define OF_MESSAGE_FLOW_MOD_COMMAND_OFFSET(version) ((version) == 1 ? 56 : 25)
51
52#define OF_MESSAGE_MIN_LENGTH 8
53#define OF_MESSAGE_MIN_STATS_LENGTH (OF_MESSAGE_STATS_TYPE_OFFSET + 2)
Rob Vaterlausb3f49d92013-10-01 17:57:31 -070054#define OF_MESSAGE_MIN_ERROR_LENGTH (OF_MESSAGE_ERROR_TYPE_OFFSET + 4)
Rich Lanea06d0c32013-03-25 08:52:03 -070055#define OF_MESSAGE_MIN_FLOW_MOD_LENGTH(version) ((version) == 1 ? 57 : 26)
56
57#define OF_MESSAGE_EXPERIMENTER_ID_OFFSET 8
58#define OF_MESSAGE_EXPERIMENTER_SUBTYPE_OFFSET 12
59#define OF_MESSAGE_EXPERIMENTER_MIN_LENGTH 16
60
61/**
62 * The "default" free message function; NULL means use nominal malloc/free
63 */
64#define OF_MESSAGE_FREE_FUNCTION NULL
65
66/**
67 * Map a message to the uint8_t * start of the message
68 */
69#define OF_MESSAGE_TO_BUFFER(msg) ((uint8_t *)(msg))
70
71/**
72 * Map a uint8_t * to a message object
73 */
74#define OF_BUFFER_TO_MESSAGE(buf) ((of_message_t)(buf))
75
76/****************************************************************
77 *
78 * Message field accessors.
79 *
80 * These do no range checking and assume a buffer with sufficient
81 * length to access the data. These are low level accessors used
82 * during the parsing and coersion stage of message processing.
83 *
84 * Fields include: version, message type, message length,
85 * transaction id, stats type (now multi-part type), experimenter id,
86 * experimenter type
87 *
88 ****************************************************************/
89
90/**
91 * @brief Get/set version of a message
92 * @param msg Pointer to the message buffer of sufficient length
93 * @param version Data for set operation
94 * @returns get returns version
95 */
96
97static inline of_version_t
98of_message_version_get(of_message_t msg) {
99 return (of_version_t)msg[OF_MESSAGE_VERSION_OFFSET];
100}
101
102static inline void
103of_message_version_set(of_message_t msg, of_version_t version) {
104 buf_u8_set(msg, (uint8_t)version);
105}
106
107/**
108 * @brief Get/set OpenFlow type of a message
109 * @param msg Pointer to the message buffer of sufficient length
110 * @param value Data for set operation
111 * @returns get returns message type
112 */
113
114static inline uint8_t
115of_message_type_get(of_message_t msg) {
116 return msg[OF_MESSAGE_TYPE_OFFSET];
117}
118
119static inline void
120of_message_type_set(of_message_t msg, uint8_t value) {
121 buf_u8_set(msg + OF_MESSAGE_TYPE_OFFSET, value);
122}
123
124/**
125 * @brief Get/set in-buffer length of a message
126 * @param msg Pointer to the message buffer of sufficient length
127 * @param len Data for set operation
128 * @returns get returns length in host order
129 */
130
131static inline uint16_t
132of_message_length_get(of_message_t msg) {
133 uint16_t val;
134 buf_u16_get(msg + OF_MESSAGE_LENGTH_OFFSET, &val);
135 return val;
136}
137
138static inline void
139of_message_length_set(of_message_t msg, uint16_t len) {
140 buf_u16_set(msg + OF_MESSAGE_LENGTH_OFFSET, len);
141}
142
143
144/**
145 * @brief Get/set transaction ID of a message
146 * @param msg Pointer to the message buffer of sufficient length
147 * @param xid Data for set operation
148 * @returns get returns xid in host order
149 */
150
151static inline uint32_t
152of_message_xid_get(of_message_t msg) {
153 uint32_t val;
154 buf_u32_get(msg + OF_MESSAGE_XID_OFFSET, &val);
155 return val;
156}
157
158static inline void
159of_message_xid_set(of_message_t msg, uint32_t xid) {
160 buf_u32_set(msg + OF_MESSAGE_XID_OFFSET, xid);
161}
162
163/**
164 * @brief Get/set stats type of a message
165 * @param msg Pointer to the message buffer of sufficient length
166 * @param type Data for set operation
167 * @returns get returns stats type in host order
168 */
169
170static inline uint16_t
171of_message_stats_type_get(of_message_t msg) {
172 uint16_t val;
173 buf_u16_get(msg + OF_MESSAGE_STATS_TYPE_OFFSET, &val);
174 return val;
175}
176
177static inline void
178of_message_stats_type_set(of_message_t msg, uint16_t type) {
179 buf_u16_set(msg + OF_MESSAGE_STATS_TYPE_OFFSET, type);
180}
181
Rob Vaterlausb3f49d92013-10-01 17:57:31 -0700182/**
183 * @brief Get/set error type of a message
184 * @param msg Pointer to the message buffer of sufficient length
185 * @param type Data for set operation
186 * @returns get returns error type in host order
187 */
188
189static inline uint16_t
190of_message_error_type_get(of_message_t msg) {
191 uint16_t val;
192 buf_u16_get(msg + OF_MESSAGE_ERROR_TYPE_OFFSET, &val);
193 return val;
194}
195
196static inline void
197of_message_error_type_set(of_message_t msg, uint16_t type) {
198 buf_u16_set(msg + OF_MESSAGE_ERROR_TYPE_OFFSET, type);
199}
200
Rich Lanea06d0c32013-03-25 08:52:03 -0700201
202/**
203 * @brief Get/set experimenter ID of a message
204 * @param msg Pointer to the message buffer of sufficient length
205 * @param experimenter_id Data for set operation
206 * @returns get returns experimenter id in host order
207 */
208
209static inline uint32_t
210of_message_experimenter_id_get(of_message_t msg) {
211 uint32_t val;
212 buf_u32_get(msg + OF_MESSAGE_EXPERIMENTER_ID_OFFSET, &val);
213 return val;
214}
215
216static inline void
217of_message_experimenter_id_set(of_message_t msg, uint32_t experimenter_id) {
218 buf_u32_set(msg + OF_MESSAGE_EXPERIMENTER_ID_OFFSET, experimenter_id);
219}
220
221
222/**
223 * @brief Get/set experimenter message type (subtype) of a message
224 * @param msg Pointer to the message buffer of sufficient length
225 * @param subtype Data for set operation
226 * @returns get returns experimenter message type in host order
227 */
228
229static inline uint32_t
230of_message_experimenter_subtype_get(of_message_t msg) {
231 uint32_t val;
232 buf_u32_get(msg + OF_MESSAGE_EXPERIMENTER_SUBTYPE_OFFSET, &val);
233 return val;
234}
235
236static inline void
237of_message_experimenter_subtype_set(of_message_t msg,
238 uint32_t subtype) {
239 buf_u32_set(msg + OF_MESSAGE_EXPERIMENTER_SUBTYPE_OFFSET,
240 subtype);
241}
242
243/**
244 * Flow mod command changed from 16 to 8 bits on the wire from 1.0 to 1.1
245 */
246static inline uint8_t
247of_message_flow_mod_command_get(of_message_t msg, of_version_t version) {
248 uint16_t val16;
249 uint8_t val8;
250
251 if (version == OF_VERSION_1_0) {
252 buf_u16_get(msg + OF_MESSAGE_FLOW_MOD_COMMAND_OFFSET(version), &val16);
253 val8 = val16;
254 } else {
255 buf_u8_get(msg + OF_MESSAGE_FLOW_MOD_COMMAND_OFFSET(version), &val8);
256 }
257 return val8;
258}
259
260static inline void
261of_message_flow_mod_command_set(of_message_t msg, of_version_t version,
262 uint8_t command) {
263 uint16_t val16;
264
265 if (version == OF_VERSION_1_0) {
266 val16 = command;
267 buf_u16_set(msg + OF_MESSAGE_FLOW_MOD_COMMAND_OFFSET(version), val16);
268 } else {
269 buf_u8_set(msg + OF_MESSAGE_FLOW_MOD_COMMAND_OFFSET(version), command);
270 }
271}
272
273#endif /* _OF_MESSAGE_H_ */