summaryrefslogtreecommitdiff
path: root/datapath-windows/ovsext/Util.c
blob: d703b2468b4ba0a0681c4378a51bcdff27494eb7 (plain)
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
/*
 * 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.
 */

#include "precomp.h"
#include "Recirc.h"
#ifdef OVS_DBG_MOD
#undef OVS_DBG_MOD
#endif
#define OVS_DBG_MOD OVS_DBG_OTHERS

#include "Debug.h"

extern NDIS_HANDLE gOvsExtDriverHandle;

_Use_decl_annotations_
VOID*
OvsAllocateMemoryWithTag(size_t size, ULONG tag)
{
    return NdisAllocateMemoryWithTagPriority(gOvsExtDriverHandle,
        (UINT32)size, tag, NormalPoolPriority);
}

VOID
OvsFreeMemoryWithTag(VOID *ptr, ULONG tag)
{
    ASSERT(ptr);
    NdisFreeMemoryWithTagPriority(gOvsExtDriverHandle, ptr, tag);
}

_Use_decl_annotations_
VOID *
OvsAllocateMemory(size_t size)
{
    return NdisAllocateMemoryWithTagPriority(gOvsExtDriverHandle,
        (UINT32)size, OVS_MEMORY_TAG, NormalPoolPriority);
}

_Use_decl_annotations_
VOID *
OvsAllocateAlignedMemory(size_t size, UINT16 align)
{
    ASSERT((align == 8) || (align == 16));

    if ((align == 8) || (align == 16)) {
        /*
         * XXX: NdisAllocateMemory*() functions don't talk anything about
         * alignment. Hence using ExAllocatePool*();
         */
        return (VOID *)ExAllocatePoolWithTagPriority(NonPagedPoolNx, size,
                                                     OVS_MEMORY_TAG,
                                                     NormalPoolPriority);
    }

    /* Invalid user input. */
    return NULL;
}

VOID
OvsFreeMemory(VOID *ptr)
{
    ASSERT(ptr);
    NdisFreeMemoryWithTagPriority(gOvsExtDriverHandle, ptr, OVS_MEMORY_TAG);
}

VOID
OvsFreeAlignedMemory(VOID *ptr)
{
    ASSERT(ptr);
    ExFreePoolWithTag(ptr, OVS_MEMORY_TAG);
}

VOID
OvsAppendList(PLIST_ENTRY dst, PLIST_ENTRY src)
{
    PLIST_ENTRY srcFirst, srcLast, dstLast;
    if (IsListEmpty(src)) {
        return;
    }
    srcFirst = src->Flink;
    srcLast = src->Blink;
    dstLast = dst->Blink;

    dstLast->Flink = srcFirst;
    srcFirst->Blink = dstLast;

    srcLast->Flink = dst;
    dst->Blink = srcLast;

    src->Flink = src;
    src->Blink = src;
}

BOOLEAN
OvsCompareString(PVOID string1, PVOID string2)
{
    /*
     * Not a super-efficient string compare since we walk over the strings
     * twice: to initialize, and then to do the comparison.
     */
    STRING str1, str2;

    RtlInitString(&str1, string1);
    RtlInitString(&str2, string2);
    return RtlEqualString(&str1, &str2, FALSE);
}

VOID *
OvsAllocateMemoryPerCpu(size_t size,
                        size_t count,
                        ULONG tag)
{
    VOID *ptr = NULL;

    ASSERT(KeQueryActiveGroupCount() == 1);

    if (!count) {
        count = KeQueryActiveProcessorCountEx(ALL_PROCESSOR_GROUPS);
    }

    ptr = OvsAllocateMemoryWithTag(count * size, tag);
    if (ptr) {
        RtlZeroMemory(ptr, count * size);
    }

    return ptr;
}

/*
 * --------------------------------------------------------------------------
 * OvsPerCpuDataInit --
 *     The function allocates necessary per-processor resources.
 * --------------------------------------------------------------------------
 */
NTSTATUS
OvsPerCpuDataInit()
{
    return OvsDeferredActionsInit();
}

/*
 * --------------------------------------------------------------------------
 * OvsPerCpuDataCleanup --
 *     The function frees all per-processor resources.
 * --------------------------------------------------------------------------
 */
VOID
OvsPerCpuDataCleanup()
{
    OvsDeferredActionsCleanup();
}

NTSTATUS
OvsIpv6StringToAddress(const char* ip6String, struct in6_addr *ipv6Addr)
{
    NTSTATUS status = STATUS_SUCCESS;
    char *terminator = NULL;

    status = RtlIpv6StringToAddressA(ip6String, &terminator, ipv6Addr);
    return status;
}

char *
OvsIpv6AddressToString(struct in6_addr ipv6Addr, char* ip6String)
{
    char *returnedIpv6Str = NULL;

    returnedIpv6Str = RtlIpv6AddressToStringA((&ipv6Addr), ip6String);
    return returnedIpv6Str;
}