1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018 Hans Petter Selasky
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/queue.h>
29 #include <sys/types.h>
30
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <time.h>
37
38 #include "int.h"
39
40 int
vring_alloc(struct virtual_ring * pvr,size_t size)41 vring_alloc(struct virtual_ring *pvr, size_t size)
42 {
43
44 if (pvr->buf_start != NULL)
45 return (EBUSY);
46 pvr->buf_start = malloc(size);
47 if (pvr->buf_start == NULL)
48 return (ENOMEM);
49 pvr->pos_read = 0;
50 pvr->total_size = size;
51 pvr->len_write = 0;
52 return (0);
53 }
54
55 void
vring_free(struct virtual_ring * pvr)56 vring_free(struct virtual_ring *pvr)
57 {
58
59 if (pvr->buf_start != NULL) {
60 free(pvr->buf_start);
61 pvr->buf_start = NULL;
62 }
63 }
64
65 void
vring_reset(struct virtual_ring * pvr)66 vring_reset(struct virtual_ring *pvr)
67 {
68 pvr->pos_read = 0;
69 pvr->len_write = 0;
70 }
71
72 void
vring_get_read(struct virtual_ring * pvr,uint8_t ** pptr,size_t * plen)73 vring_get_read(struct virtual_ring *pvr, uint8_t **pptr, size_t *plen)
74 {
75 uint32_t delta;
76
77 if (pvr->buf_start == NULL) {
78 *pptr = NULL;
79 *plen = 0;
80 return;
81 }
82 delta = pvr->total_size - pvr->pos_read;
83 if (delta > pvr->len_write)
84 delta = pvr->len_write;
85
86 *pptr = pvr->buf_start + pvr->pos_read;
87 *plen = delta;
88 }
89
90 void
vring_get_write(struct virtual_ring * pvr,uint8_t ** pptr,size_t * plen)91 vring_get_write(struct virtual_ring *pvr, uint8_t **pptr, size_t *plen)
92 {
93 uint32_t delta;
94 uint32_t len_read;
95 uint32_t pos_write;
96
97 if (pvr->buf_start == NULL) {
98 *pptr = NULL;
99 *plen = 0;
100 return;
101 }
102 pos_write = pvr->pos_read + pvr->len_write;
103 if (pos_write >= pvr->total_size)
104 pos_write -= pvr->total_size;
105
106 len_read = pvr->total_size - pvr->len_write;
107
108 delta = pvr->total_size - pos_write;
109 if (delta > len_read)
110 delta = len_read;
111
112 *pptr = pvr->buf_start + pos_write;
113 *plen = delta;
114 }
115
116 void
vring_inc_read(struct virtual_ring * pvr,size_t len)117 vring_inc_read(struct virtual_ring *pvr, size_t len)
118 {
119
120 pvr->pos_read += len;
121 pvr->len_write -= len;
122
123 /* check for wrap-around */
124 if (pvr->pos_read == pvr->total_size)
125 pvr->pos_read = 0;
126 }
127
128 void
vring_inc_write(struct virtual_ring * pvr,size_t len)129 vring_inc_write(struct virtual_ring *pvr, size_t len)
130 {
131
132 pvr->len_write += len;
133 }
134
135 size_t
vring_total_read_len(struct virtual_ring * pvr)136 vring_total_read_len(struct virtual_ring *pvr)
137 {
138
139 return (pvr->len_write);
140 }
141
142 size_t
vring_total_write_len(struct virtual_ring * pvr)143 vring_total_write_len(struct virtual_ring *pvr)
144 {
145
146 return (pvr->total_size - pvr->len_write);
147 }
148
149 size_t
vring_write_linear(struct virtual_ring * pvr,const uint8_t * src,size_t total)150 vring_write_linear(struct virtual_ring *pvr, const uint8_t *src, size_t total)
151 {
152 uint8_t *buf_ptr;
153 size_t buf_len;
154 size_t sum = 0;
155
156 while (total != 0) {
157 vring_get_write(pvr, &buf_ptr, &buf_len);
158 if (buf_len == 0)
159 break;
160 if (buf_len > total)
161 buf_len = total;
162 memcpy(buf_ptr, src, buf_len);
163 vring_inc_write(pvr, buf_len);
164 src += buf_len;
165 sum += buf_len;
166 total -= buf_len;
167 }
168 return (sum);
169 }
170
171 size_t
vring_read_linear(struct virtual_ring * pvr,uint8_t * dst,size_t total)172 vring_read_linear(struct virtual_ring *pvr, uint8_t *dst, size_t total)
173 {
174 uint8_t *buf_ptr;
175 size_t buf_len;
176 size_t sum = 0;
177
178 if (total > vring_total_read_len(pvr))
179 return (0);
180
181 while (total != 0) {
182 vring_get_read(pvr, &buf_ptr, &buf_len);
183 if (buf_len == 0)
184 break;
185 if (buf_len > total)
186 buf_len = total;
187 memcpy(dst, buf_ptr, buf_len);
188 vring_inc_read(pvr, buf_len);
189 dst += buf_len;
190 sum += buf_len;
191 total -= buf_len;
192 }
193 return (sum);
194 }
195
196 size_t
vring_write_zero(struct virtual_ring * pvr,size_t total)197 vring_write_zero(struct virtual_ring *pvr, size_t total)
198 {
199 uint8_t *buf_ptr;
200 size_t buf_len;
201 size_t sum = 0;
202
203 while (total != 0) {
204 vring_get_write(pvr, &buf_ptr, &buf_len);
205 if (buf_len == 0)
206 break;
207 if (buf_len > total)
208 buf_len = total;
209 memset(buf_ptr, 0, buf_len);
210 vring_inc_write(pvr, buf_len);
211 sum += buf_len;
212 total -= buf_len;
213 }
214 return (sum);
215 }
216