Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * buffer.c: allocate/free network buffers. |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | |
| 42 | static void |
| 43 | vlib_serialize_tx (serialize_main_header_t * m, serialize_stream_t * s) |
| 44 | { |
| 45 | vlib_main_t *vm; |
| 46 | vlib_serialize_buffer_main_t *sm; |
| 47 | uword n, n_bytes_to_write; |
| 48 | vlib_buffer_t *last; |
| 49 | |
| 50 | n_bytes_to_write = s->current_buffer_index; |
| 51 | sm = |
| 52 | uword_to_pointer (s->data_function_opaque, |
| 53 | vlib_serialize_buffer_main_t *); |
| 54 | vm = sm->vlib_main; |
| 55 | |
| 56 | ASSERT (sm->tx.max_n_data_bytes_per_chain > 0); |
| 57 | if (serialize_stream_is_end_of_stream (s) |
| 58 | || sm->tx.n_total_data_bytes + n_bytes_to_write > |
| 59 | sm->tx.max_n_data_bytes_per_chain) |
| 60 | { |
| 61 | vlib_process_t *p = vlib_get_current_process (vm); |
| 62 | |
| 63 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 64 | last->current_length = n_bytes_to_write; |
| 65 | |
| 66 | vlib_set_next_frame_buffer (vm, &p->node_runtime, sm->tx.next_index, |
| 67 | sm->first_buffer); |
| 68 | |
| 69 | sm->first_buffer = sm->last_buffer = ~0; |
| 70 | sm->tx.n_total_data_bytes = 0; |
| 71 | } |
| 72 | |
| 73 | else if (n_bytes_to_write == 0 && s->n_buffer_bytes == 0) |
| 74 | { |
| 75 | ASSERT (sm->first_buffer == ~0); |
| 76 | ASSERT (sm->last_buffer == ~0); |
| 77 | n = |
| 78 | vlib_buffer_alloc_from_free_list (vm, &sm->first_buffer, 1, |
| 79 | sm->tx.free_list_index); |
| 80 | if (n != 1) |
| 81 | serialize_error (m, |
| 82 | clib_error_create |
| 83 | ("vlib_buffer_alloc_from_free_list fails")); |
| 84 | sm->last_buffer = sm->first_buffer; |
| 85 | s->n_buffer_bytes = |
| 86 | vlib_buffer_free_list_buffer_size (vm, sm->tx.free_list_index); |
| 87 | } |
| 88 | |
| 89 | if (n_bytes_to_write > 0) |
| 90 | { |
| 91 | vlib_buffer_t *prev = vlib_get_buffer (vm, sm->last_buffer); |
| 92 | n = |
| 93 | vlib_buffer_alloc_from_free_list (vm, &sm->last_buffer, 1, |
| 94 | sm->tx.free_list_index); |
| 95 | if (n != 1) |
| 96 | serialize_error (m, |
| 97 | clib_error_create |
| 98 | ("vlib_buffer_alloc_from_free_list fails")); |
| 99 | sm->tx.n_total_data_bytes += n_bytes_to_write; |
| 100 | prev->current_length = n_bytes_to_write; |
| 101 | prev->next_buffer = sm->last_buffer; |
| 102 | prev->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 103 | } |
| 104 | |
| 105 | if (sm->last_buffer != ~0) |
| 106 | { |
| 107 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 108 | s->buffer = vlib_buffer_get_current (last); |
| 109 | s->current_buffer_index = 0; |
| 110 | ASSERT (last->current_data == s->current_buffer_index); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | vlib_serialize_rx (serialize_main_header_t * m, serialize_stream_t * s) |
| 116 | { |
| 117 | vlib_main_t *vm; |
| 118 | vlib_serialize_buffer_main_t *sm; |
| 119 | vlib_buffer_t *last; |
| 120 | |
| 121 | sm = |
| 122 | uword_to_pointer (s->data_function_opaque, |
| 123 | vlib_serialize_buffer_main_t *); |
| 124 | vm = sm->vlib_main; |
| 125 | |
| 126 | if (serialize_stream_is_end_of_stream (s)) |
| 127 | return; |
| 128 | |
| 129 | if (sm->last_buffer != ~0) |
| 130 | { |
| 131 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 132 | |
| 133 | if (last->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 134 | sm->last_buffer = last->next_buffer; |
| 135 | else |
| 136 | { |
| 137 | vlib_buffer_free (vm, &sm->first_buffer, /* count */ 1); |
| 138 | sm->first_buffer = sm->last_buffer = ~0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (sm->last_buffer == ~0) |
| 143 | { |
| 144 | while (clib_fifo_elts (sm->rx.buffer_fifo) == 0) |
| 145 | { |
| 146 | sm->rx.ready_one_time_event = |
| 147 | vlib_process_create_one_time_event (vm, vlib_current_process (vm), |
| 148 | ~0); |
| 149 | vlib_process_wait_for_one_time_event (vm, /* no event data */ 0, |
| 150 | sm->rx.ready_one_time_event); |
| 151 | } |
| 152 | |
| 153 | clib_fifo_sub1 (sm->rx.buffer_fifo, sm->first_buffer); |
| 154 | sm->last_buffer = sm->first_buffer; |
| 155 | } |
| 156 | |
| 157 | ASSERT (sm->last_buffer != ~0); |
| 158 | |
| 159 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 160 | s->current_buffer_index = 0; |
| 161 | s->buffer = vlib_buffer_get_current (last); |
| 162 | s->n_buffer_bytes = last->current_length; |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | serialize_open_vlib_helper (serialize_main_t * m, |
| 167 | vlib_main_t * vm, |
| 168 | vlib_serialize_buffer_main_t * sm, uword is_read) |
| 169 | { |
| 170 | /* Initialize serialize main but save overflow buffer for re-use between calls. */ |
| 171 | { |
| 172 | u8 *save = m->stream.overflow_buffer; |
| 173 | memset (m, 0, sizeof (m[0])); |
| 174 | m->stream.overflow_buffer = save; |
| 175 | if (save) |
| 176 | _vec_len (save) = 0; |
| 177 | } |
| 178 | |
| 179 | sm->first_buffer = sm->last_buffer = ~0; |
| 180 | if (is_read) |
| 181 | clib_fifo_reset (sm->rx.buffer_fifo); |
| 182 | else |
| 183 | sm->tx.n_total_data_bytes = 0; |
| 184 | sm->vlib_main = vm; |
| 185 | m->header.data_function = is_read ? vlib_serialize_rx : vlib_serialize_tx; |
| 186 | m->stream.data_function_opaque = pointer_to_uword (sm); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | serialize_open_vlib_buffer (serialize_main_t * m, vlib_main_t * vm, |
| 191 | vlib_serialize_buffer_main_t * sm) |
| 192 | { |
| 193 | serialize_open_vlib_helper (m, vm, sm, /* is_read */ 0); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | unserialize_open_vlib_buffer (serialize_main_t * m, vlib_main_t * vm, |
| 198 | vlib_serialize_buffer_main_t * sm) |
| 199 | { |
| 200 | serialize_open_vlib_helper (m, vm, sm, /* is_read */ 1); |
| 201 | } |
| 202 | |
| 203 | u32 |
| 204 | serialize_close_vlib_buffer (serialize_main_t * m) |
| 205 | { |
| 206 | vlib_serialize_buffer_main_t *sm |
| 207 | = uword_to_pointer (m->stream.data_function_opaque, |
| 208 | vlib_serialize_buffer_main_t *); |
| 209 | vlib_buffer_t *last; |
| 210 | serialize_stream_t *s = &m->stream; |
| 211 | |
| 212 | last = vlib_get_buffer (sm->vlib_main, sm->last_buffer); |
| 213 | last->current_length = s->current_buffer_index; |
| 214 | |
| 215 | if (vec_len (s->overflow_buffer) > 0) |
| 216 | { |
| 217 | sm->last_buffer |
| 218 | = vlib_buffer_add_data (sm->vlib_main, sm->tx.free_list_index, |
Dave Barach | 0d65d11 | 2018-03-16 15:21:35 -0400 | [diff] [blame] | 219 | sm->last_buffer, |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 220 | s->overflow_buffer, |
| 221 | vec_len (s->overflow_buffer)); |
| 222 | _vec_len (s->overflow_buffer) = 0; |
| 223 | } |
| 224 | |
| 225 | return sm->first_buffer; |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | unserialize_close_vlib_buffer (serialize_main_t * m) |
| 230 | { |
| 231 | vlib_serialize_buffer_main_t *sm |
| 232 | = uword_to_pointer (m->stream.data_function_opaque, |
| 233 | vlib_serialize_buffer_main_t *); |
| 234 | if (sm->first_buffer != ~0) |
| 235 | vlib_buffer_free_one (sm->vlib_main, sm->first_buffer); |
| 236 | clib_fifo_reset (sm->rx.buffer_fifo); |
| 237 | if (m->stream.overflow_buffer) |
| 238 | _vec_len (m->stream.overflow_buffer) = 0; |
| 239 | } |
| 240 | |
| 241 | /** @endcond */ |
| 242 | /* |
| 243 | * fd.io coding-style-patch-verification: ON |
| 244 | * |
| 245 | * Local Variables: |
| 246 | * eval: (c-set-style "gnu") |
| 247 | * End: |
| 248 | */ |