blob: 054dc01c58e7f9d037fee927a3db9eea5a8dee9a [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 *
32 * of_wire_buf.c
33 *
34 * Implementation of more complicated of_wire_buf operations
35 *
36 ****************************************************************/
37
38#include <loci/loci.h>
39
40#if 0
41static of_match_v1_t *
42wire_buf_v1_to_match(of_wire_buffer_t *wbuf, int offset)
43{
44 of_match_v1_t *match;
45 match = of_match_v1_new(OF_VERSION_1_0);
46 return match;
47}
48
49/*
50 * First pass at wire buf to match conversions. These involve a lot
51 * of copying and could be made more efficient.
52 */
53int
54of_wire_buffer_of_match_get(of_object_t *obj, int offset, of_match_t *match)
55{
56 switch (obj->version) {
57 case OF_VERSION_1_0:
58 break;
59 case OF_VERSION_1_1:
60 break;
61 case OF_VERSION_1_2:
62 break;
63 default:
64 return OF_ERROR_VERSION;
65 break;
66 }
67
68 return OF_ERROR_NONE;
69}
70
71/**
72 * Write a host match structure into the wire buffer
73 * @param obj The object that owns the buffer to write to
74 * @param offset The start location in the wire buffer
75 * @param match Pointer to the host match object
76 * @param cur_len The current length of the object in the buffer
77 *
78 * If the current length is different than the length of the new data
79 * being written, then move the remains of the buffer. This only applies
80 * to version 1.2 (though it should apply to 1.1).
81 */
82
83int
84of_wire_buffer_of_match_set(of_object_t *obj, int offset,
85 of_match_t *match, int cur_len)
86{
87 // of_octets_t octets;
88
89 switch (obj->version) {
90 case OF_VERSION_1_0:
91 break;
92 case OF_VERSION_1_1:
93 break;
94 case OF_VERSION_1_2:
95 // of_match_v3_serialize(match, &octets);
96 break;
97 default:
98 return OF_ERROR_VERSION;
99 break;
100 }
101
102 return OF_ERROR_NONE;
103}
104#endif
105
106/**
107 * Replace data in the data buffer, possibly with a new
108 * length or appending to buffer.
109 *
110 * @param wbuf The wire buffer being updated.
111 * @param offset The start point of the update
112 * @param old_len The number of bytes being replaced
113 * @param data Source of bytes to write into the buffer
114 * @param new_len The number of bytes to write
115 *
116 * The buffer may grow for this operation. Current byte count
117 * is pre-grow for the replace.
118 *
119 * The current byte count for the buffer is updated.
120 *
121 */
122
123void
124of_wire_buffer_replace_data(of_wire_buffer_t *wbuf,
125 int offset,
126 int old_len,
127 uint8_t *data,
128 int new_len)
129{
130 int bytes;
131 uint8_t *src_ptr, *dst_ptr;
132 int cur_bytes;
133
134 ASSERT(wbuf != NULL);
135
136 cur_bytes = wbuf->current_bytes;
137
138 /* Doesn't make sense; mismatch in current buffer info */
139 ASSERT(old_len + offset <= wbuf->current_bytes);
140
141 if (old_len < new_len) {
142 of_wire_buffer_grow(wbuf, offset + new_len);
143 } else {
144 wbuf->current_bytes += (new_len - old_len); // may decrease size
145 }
146
147 if ((old_len + offset < cur_bytes) && (old_len != new_len)) {
148 /* Need to move back of buffer */
149 src_ptr = &wbuf->buf[offset + old_len];
150 dst_ptr = &wbuf->buf[offset + new_len];
151 bytes = cur_bytes - (offset + old_len);
152 MEMMOVE(dst_ptr, src_ptr, bytes);
153 }
154
155 dst_ptr = &wbuf->buf[offset];
156 MEMCPY(dst_ptr, data, new_len);
157
158 ASSERT(wbuf->current_bytes == cur_bytes + (new_len - old_len));
159}