1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* Copyright (c) 2014 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* ==========================================================================
* This is a simple buffer mangement framework specific for netlink protocol.
* The name could be confused with ovsext/BufferMgmt.c. Ovsext/BufferMgmt.c
* deals with buffer mgmt for NBLs. Where as this framework deals with
* management of buffer that holds a netlink message.
*
* This framework provides APIs for putting/accessing data in a buffer. These
* APIs are used by driver's netlink protocol implementation.
*
* We can see this framework as a subset of ofpbuf in ovs userspace.
*
* This framework is NOT a generic buffer management framework (ofpbuf
* is a generic buffer mgmt framework) and provides only the functioanlities
* which would be useful for netlink protocol. Some of the key features are:
*
* a. It DOES NOT support automatic buffer reallocation.
* i. A netlink input/output message is a static buffer.
* b. The unused space is at the tail.
* c. There is no notion of headdroom.
* ==========================================================================
*/
#include <ndis.h>
#include <netiodef.h>
#include <intsafe.h>
#include <ntintsafe.h>
#include <ntstrsafe.h>
#ifdef OVS_DBG_MOD
#undef OVS_DBG_MOD
#endif
#define OVS_DBG_MOD OVS_DBG_NETLINK
#include "Debug.h"
#include "NetlinkBuf.h"
/* Returns used space in the buffer */
#define NL_BUF_USED_SPACE(_buf) (_buf->bufLen - \
_buf->bufRemLen)
/* Validates that offset is within buffer boundaries and will not
* create holes in the buffer.*/
#define NL_BUF_IS_VALID_OFFSET(_buf, _offset) (_offset <= \
NL_BUF_TAIL_OFFSET(_buf) ? 1 : 0)
/* Validates if new data of size _size can be added at offset _offset.
* This macor assumes that offset validation has been done.*/
#define NL_BUF_CAN_ADD(_buf, _size, _offset) (((_offset + _size <= \
_buf->bufLen) && (_size \
<= _buf->bufRemLen)) ? \
1 : 0)
/* Returns the offset of tail wrt buffer head */
#define NL_BUF_TAIL_OFFSET(_buf) (_buf->tail - _buf->head)
static __inline VOID
_NlBufCopyAtTailUnsafe(PNL_BUFFER nlBuf, PCHAR data, UINT32 len);
static __inline VOID
_NlBufCopyAtOffsetUnsafe(PNL_BUFFER nlBuf, PCHAR data,
UINT32 len, UINT32 offset);
/*
* --------------------------------------------------------------------------
* NlBufInit --
*
* Initializes NL_BUF with buffer pointer and length.
* --------------------------------------------------------------------------
*/
VOID
NlBufInit(PNL_BUFFER nlBuf, PCHAR base, UINT32 size)
{
ASSERT(nlBuf);
nlBuf->head = nlBuf->tail = base;
nlBuf->bufLen = nlBuf->bufRemLen = size;
}
/*
* --------------------------------------------------------------------------
* NlBufDeInit --
*
* Resets the buffer variables to NULL.
* --------------------------------------------------------------------------
*/
VOID
NlBufDeInit(PNL_BUFFER nlBuf)
{
ASSERT(nlBuf);
nlBuf->head = nlBuf->tail = NULL;
nlBuf->bufLen = nlBuf->bufRemLen = 0;
}
/*
* --------------------------------------------------------------------------
* NlBufCopyAtTail --
*
* Copies the data to the tail end of the buffer.
* --------------------------------------------------------------------------
*/
BOOLEAN
NlBufCopyAtTail(PNL_BUFFER nlBuf, PCHAR data, UINT32 len)
{
BOOLEAN ret = TRUE;
ASSERT(nlBuf);
/* Check if we have enough space */
if (!NL_BUF_CAN_ADD(nlBuf, len, NL_BUF_TAIL_OFFSET(nlBuf))) {
ret = FALSE;
goto done;
}
_NlBufCopyAtTailUnsafe(nlBuf, data, len);
done:
return ret;
}
/*
* --------------------------------------------------------------------------
* NlBufCopyAtHead --
*
* Copies the data to the head of the buffer.
* It can be seen as special case of NlBufCopyAtOffset with input
* offset zero.
* --------------------------------------------------------------------------
*/
BOOLEAN
NlBufCopyAtHead(PNL_BUFFER nlBuf, PCHAR data, UINT32 len)
{
BOOLEAN ret = TRUE;
ASSERT(nlBuf);
/* Check if we have enough space */
if (!NL_BUF_CAN_ADD(nlBuf, len, 0)) {
ret = FALSE;
goto done;
}
if (nlBuf->head == nlBuf->tail) {
/* same as inserting in tail */
_NlBufCopyAtTailUnsafe(nlBuf, data, len);
goto done;
}
_NlBufCopyAtOffsetUnsafe(nlBuf, data, len, 0);
done:
return ret;
}
/*
* --------------------------------------------------------------------------
* NlBufCopyAtOffset --
*
* Inserts data at input offset in the buffer.
* If the offset is earlier then tail end then it first creates
* space of size input length at input offset by moving the existing
* data forward.
* --------------------------------------------------------------------------
*/
BOOLEAN
NlBufCopyAtOffset(PNL_BUFFER nlBuf, PCHAR data, UINT32 len, UINT32 offset)
{
PCHAR dest = NULL;
BOOLEAN ret = TRUE;
ASSERT(nlBuf);
/* Check if input offset is valid and has enough space */
if ((!NL_BUF_IS_VALID_OFFSET(nlBuf, offset)) ||
(!NL_BUF_CAN_ADD(nlBuf, len, offset))) {
ret = FALSE;
goto done;
}
dest = nlBuf->head + offset;
if (dest == nlBuf->tail) {
/* same as inserting in tail */
_NlBufCopyAtTailUnsafe(nlBuf, data, len);
goto done;
}
_NlBufCopyAtOffsetUnsafe(nlBuf, data, len, offset);
done:
return ret;
}
/*
* --------------------------------------------------------------------------
* NlBufCopyAtTailUninit --
*
* Memsets the buffer portion of length len at tail end with zero.
* --------------------------------------------------------------------------
*/
PCHAR
NlBufCopyAtTailUninit(PNL_BUFFER nlBuf, UINT32 len)
{
PCHAR ret;
ret = nlBuf->tail;
if ((NlBufCopyAtTail(nlBuf, NULL, len)) == FALSE) {
ret = NULL;
}
return ret;
}
/*
* --------------------------------------------------------------------------
* NlBufCopyAtHeadUninit --
*
* Memsets the buffer portion of length len at head with zero.
* --------------------------------------------------------------------------
*/
PCHAR
NlBufCopyAtHeadUninit(PNL_BUFFER nlBuf, UINT32 len)
{
PCHAR ret = NULL;
if ((NlBufCopyAtHead(nlBuf, NULL, len)) == FALSE) {
goto done;
}
ret = nlBuf->head;
done:
return ret;
}
/*
* --------------------------------------------------------------------------
* NlBufCopyAtOffsetUninit --
*
* Memsets the buffer portion of length len at head with zero.
*
* If the offset is earlier then tail end then it first creates
* space of size input length at input offset by moving the existing
* data forward.
* --------------------------------------------------------------------------
*/
PCHAR
NlBufCopyAtOffsetUninit(PNL_BUFFER nlBuf, UINT32 len, UINT32 offset)
{
PCHAR ret = NULL;
if ((NlBufCopyAtOffset(nlBuf, NULL, len, offset)) == FALSE) {
goto done;
}
ret = nlBuf->head + offset;
done:
return ret;
}
/*
* --------------------------------------------------------------------------
* NlBufAt --
*
* Returns pointer to buffer at input offset.
* bufLen is used to verify that expected data length
* is within valid boundaries. Here by boundaries we mean
* within head and tail.
* --------------------------------------------------------------------------
*/
PCHAR
NlBufAt(PNL_BUFFER nlBuf, UINT32 offset, UINT32 bufLen)
{
PCHAR ret = NULL;
ASSERT(nlBuf);
if ((!NL_BUF_IS_VALID_OFFSET(nlBuf, offset))) {
goto done;
}
/* Check if requested buffer is within head and tail */
if ((offset + bufLen) > NL_BUF_USED_SPACE(nlBuf)) {
goto done;
}
ret = nlBuf->head + offset;
done:
return ret;
}
/* *_Unsafe functions does not do any validation. */
/*
* --------------------------------------------------------------------------
* _NlBufCopyAtTailUnsafe --
*
* Helper function for NlBufCopyAtTail.
* --------------------------------------------------------------------------
*/
static __inline VOID
_NlBufCopyAtTailUnsafe(PNL_BUFFER nlBuf, PCHAR data, UINT32 len)
{
if (data) {
RtlCopyMemory(nlBuf->tail, data, len);
} else {
RtlZeroMemory(nlBuf->tail, len);
}
nlBuf->tail += len;
nlBuf->bufRemLen -= len;
}
/*
* --------------------------------------------------------------------------
* _NlBufCopyAtOffsetUnsafe --
*
* Helper function for NlBufCopyAtOffset.
* --------------------------------------------------------------------------
*/
static __inline VOID
_NlBufCopyAtOffsetUnsafe(PNL_BUFFER nlBuf, PCHAR data,
UINT32 len, UINT32 offset)
{
PCHAR dest = NULL;
dest = nlBuf->head + offset;
RtlMoveMemory(dest+len, dest, NL_BUF_USED_SPACE(nlBuf) - offset);
if (data) {
RtlCopyMemory(dest, data, len);
} else {
RtlZeroMemory(dest, len);
}
nlBuf->tail += len;
nlBuf->bufRemLen -= len;
}
|