Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | :: # 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 Lane | d983aa5 | 2013-06-13 11:48:37 -0700 | [diff] [blame] | 28 | :: include('_copyright.c') |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 29 | |
| 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 |
| 41 | static of_match_v1_t * |
| 42 | wire_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 | */ |
| 53 | int |
| 54 | of_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 | |
| 83 | int |
| 84 | of_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 | |
| 123 | void |
| 124 | of_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 | |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 134 | LOCI_ASSERT(wbuf != NULL); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 135 | |
| 136 | cur_bytes = wbuf->current_bytes; |
| 137 | |
| 138 | /* Doesn't make sense; mismatch in current buffer info */ |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 139 | LOCI_ASSERT(old_len + offset <= wbuf->current_bytes); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 140 | |
Rich Lane | 7be2e61 | 2013-07-18 11:46:58 -0700 | [diff] [blame] | 141 | wbuf->current_bytes += (new_len - old_len); // may decrease size |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 142 | |
| 143 | if ((old_len + offset < cur_bytes) && (old_len != new_len)) { |
| 144 | /* Need to move back of buffer */ |
| 145 | src_ptr = &wbuf->buf[offset + old_len]; |
| 146 | dst_ptr = &wbuf->buf[offset + new_len]; |
| 147 | bytes = cur_bytes - (offset + old_len); |
| 148 | MEMMOVE(dst_ptr, src_ptr, bytes); |
| 149 | } |
| 150 | |
| 151 | dst_ptr = &wbuf->buf[offset]; |
| 152 | MEMCPY(dst_ptr, data, new_len); |
| 153 | |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 154 | LOCI_ASSERT(wbuf->current_bytes == cur_bytes + (new_len - old_len)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 155 | } |