Skip to content

Commit

Permalink
vrouter: initialize sandesh; implement random() functions
Browse files Browse the repository at this point in the history
Changes required to support Contrail on Windows:
- windows/vr_win_transport.c - functions used to initialize sandesh and
  vr_message layer on Windows
- windows/windows_random.c - implement get_random_bytes() and
  get_random_ulong() for Windows

Initial work:
  https://github.com/codilime/contrail-vrouter/commits/windows

Change-Id: I6e0d7b24c6d43f5657bd751f53c721a5397289a5
Partial-Bug: #1734699
  • Loading branch information
Dariusz Sosnowski committed Dec 1, 2017
1 parent 7e1320e commit fbfd4b4
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
81 changes: 81 additions & 0 deletions windows/vr_win_transport.c
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
*/
#include <precomp.h>

#include "vr_message.h"
#include "vr_sandesh.h"

static ULONG WIN_TRANSPORT_TAG = 'ARTV';

static char *
win_trans_alloc(unsigned int size)
{
char *buffer;
size_t allocation_size;

allocation_size = NLMSG_ALIGN(size) + NETLINK_HEADER_LEN;
buffer = ExAllocatePoolWithTag(NonPagedPoolNx, allocation_size, WIN_TRANSPORT_TAG);
if (buffer == NULL)
return NULL;

return buffer + NETLINK_HEADER_LEN;
}

static void
win_trans_free(char *buf)
{
ASSERT(buf != NULL);
ExFreePool(buf - NETLINK_HEADER_LEN);
}

static struct vr_mtransport win_transport = {
.mtrans_alloc = win_trans_alloc,
.mtrans_free = win_trans_free,
};

void
vr_transport_exit(void)
{
vr_message_transport_unregister(&win_transport);
}

int
vr_transport_init(void)
{
int ret;

ret = vr_message_transport_register(&win_transport);
if (ret) {
DbgPrint("%s: error on transport register = %d\n", __func__, ret);
return ret;
}

return 0;
}

NTSTATUS
vr_message_init(void)
{
int ret = vr_sandesh_init();
if (ret) {
DbgPrint("%s: vr_sandesh_init() failed with return %d\n", __func__, ret);
return NDIS_STATUS_FAILURE;
}

ret = vr_transport_init();
if (ret) {
DbgPrint("%s: vr_transport_init() failed with return %d", __func__, ret);
vr_sandesh_exit();
return NDIS_STATUS_FAILURE;
}

return NDIS_STATUS_SUCCESS;
}

void
vr_message_exit(void)
{
vr_transport_exit();
vr_sandesh_exit();
}
35 changes: 35 additions & 0 deletions windows/windows_random.c
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
*/
#include "precomp.h"
#include "vr_os.h"

ULONG seed;
bool isSeedInitialized;

static void prepareSeed() {
if (!isSeedInitialized) {
seed = KeQueryPerformanceCounter(NULL).LowPart;
isSeedInitialized = TRUE;
}
}

// make sure prepareSeed has been run before using this function
static ULONG get_random_ulong() {
const ULONG a = 1103515245UL, c = 12345UL;
seed = a * seed + c;
return seed;
}

void get_random_bytes(void *buf, int nbytes) {
ULONG t;
prepareSeed();
while (nbytes > sizeof(ULONG)) {
t = get_random_ulong();
memcpy(buf, &t, sizeof(ULONG));
nbytes -= sizeof(ULONG);
buf = (PINT8)buf + sizeof(ULONG);
}
t = get_random_ulong();
memcpy(buf, &t, nbytes);
}

0 comments on commit fbfd4b4

Please sign in to comment.