2 * Copyright (c) 2014 VMware, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * This file contains the implementation of the management functionality of the
34 #define OVS_DBG_MOD OVS_DBG_SWITCH
37 POVS_SWITCH_CONTEXT gOvsSwitchContext;
38 LONG volatile gOvsInAttach;
39 UINT64 ovsTimeIncrementPerTick;
41 extern NDIS_HANDLE gOvsExtDriverHandle;
42 extern NDIS_HANDLE gOvsExtDriverObject;
43 extern PDEVICE_OBJECT gOvsDeviceObject;
46 * Reference count used to prevent premature deallocation of the global switch
47 * context structure, gOvsSwitchContext.
49 volatile LONG gOvsSwitchContextRefCount = 1;
51 static NDIS_STATUS OvsCreateSwitch(NDIS_HANDLE ndisFilterHandle,
52 POVS_SWITCH_CONTEXT *switchContextOut);
53 static NDIS_STATUS OvsInitSwitchContext(POVS_SWITCH_CONTEXT switchContext);
54 static VOID OvsDeleteSwitch(POVS_SWITCH_CONTEXT switchContext);
55 static VOID OvsUninitSwitchContext(POVS_SWITCH_CONTEXT switchContext);
56 static NDIS_STATUS OvsActivateSwitch(POVS_SWITCH_CONTEXT switchContext);
60 * --------------------------------------------------------------------------
61 * Implements filter driver's FilterAttach function.
63 * This function allocates the switch context, and initializes its necessary
65 * --------------------------------------------------------------------------
68 OvsExtAttach(NDIS_HANDLE ndisFilterHandle,
69 NDIS_HANDLE filterDriverContext,
70 PNDIS_FILTER_ATTACH_PARAMETERS attachParameters)
72 NDIS_STATUS status = NDIS_STATUS_FAILURE;
73 NDIS_FILTER_ATTRIBUTES ovsExtAttributes;
74 POVS_SWITCH_CONTEXT switchContext = NULL;
76 UNREFERENCED_PARAMETER(filterDriverContext);
78 OVS_LOG_TRACE("Enter: ndisFilterHandle %p", ndisFilterHandle);
80 ASSERT(filterDriverContext == (NDIS_HANDLE)gOvsExtDriverObject);
81 if (attachParameters->MiniportMediaType != NdisMedium802_3) {
82 status = NDIS_STATUS_INVALID_PARAMETER;
86 if (gOvsExtDriverHandle == NULL) {
87 OVS_LOG_TRACE("Exit: OVSEXT driver is not loaded.");
92 if (gOvsSwitchContext) {
93 OVS_LOG_TRACE("Exit: Failed to create OVS Switch, only one datapath is"
94 "supported, %p.", gOvsSwitchContext);
98 if (InterlockedCompareExchange(&gOvsInAttach, 1, 0)) {
99 /* Just fail the request. */
100 OVS_LOG_TRACE("Exit: Failed to create OVS Switch, since another attach"
101 "instance is in attach process.");
105 status = OvsInitIpHelper(ndisFilterHandle);
106 if (status != STATUS_SUCCESS) {
107 OVS_LOG_ERROR("Exit: Failed to initialize IP helper.");
111 status = OvsCreateSwitch(ndisFilterHandle, &switchContext);
112 if (status != NDIS_STATUS_SUCCESS) {
113 OvsCleanupIpHelper();
116 ASSERT(switchContext);
119 * Register the switch context with NDIS so NDIS can pass it back to the
120 * FilterXXX callback functions as the 'FilterModuleContext' parameter.
122 RtlZeroMemory(&ovsExtAttributes, sizeof(NDIS_FILTER_ATTRIBUTES));
123 ovsExtAttributes.Header.Revision = NDIS_FILTER_ATTRIBUTES_REVISION_1;
124 ovsExtAttributes.Header.Size = sizeof(NDIS_FILTER_ATTRIBUTES);
125 ovsExtAttributes.Header.Type = NDIS_OBJECT_TYPE_FILTER_ATTRIBUTES;
126 ovsExtAttributes.Flags = 0;
128 NDIS_DECLARE_FILTER_MODULE_CONTEXT(OVS_SWITCH_CONTEXT);
129 status = NdisFSetAttributes(ndisFilterHandle, switchContext, &ovsExtAttributes);
130 if (status != NDIS_STATUS_SUCCESS) {
131 OVS_LOG_ERROR("Failed to set attributes.");
132 OvsCleanupIpHelper();
136 /* Setup the state machine. */
137 switchContext->controlFlowState = OvsSwitchAttached;
138 switchContext->dataFlowState = OvsSwitchPaused;
140 gOvsSwitchContext = switchContext;
144 gOvsInAttach = FALSE;
145 if (status != NDIS_STATUS_SUCCESS) {
146 if (switchContext != NULL) {
147 OvsDeleteSwitch(switchContext);
150 OVS_LOG_TRACE("Exit: status %x", status);
157 * --------------------------------------------------------------------------
158 * This function allocated the switch context, and initializes its necessary
160 * --------------------------------------------------------------------------
163 OvsCreateSwitch(NDIS_HANDLE ndisFilterHandle,
164 POVS_SWITCH_CONTEXT *switchContextOut)
167 POVS_SWITCH_CONTEXT switchContext;
168 NDIS_SWITCH_CONTEXT hostSwitchContext;
169 NDIS_SWITCH_OPTIONAL_HANDLERS hostSwitchHandler;
171 OVS_LOG_TRACE("Enter: Create switch object");
173 switchContext = (POVS_SWITCH_CONTEXT) OvsAllocateMemoryWithTag(
174 sizeof(OVS_SWITCH_CONTEXT), OVS_SWITCH_POOL_TAG);
175 if (switchContext == NULL) {
176 status = NDIS_STATUS_RESOURCES;
177 goto create_switch_done;
179 RtlZeroMemory(switchContext, sizeof(OVS_SWITCH_CONTEXT));
181 /* Initialize the switch. */
182 hostSwitchHandler.Header.Type = NDIS_OBJECT_TYPE_SWITCH_OPTIONAL_HANDLERS;
183 hostSwitchHandler.Header.Size = NDIS_SIZEOF_SWITCH_OPTIONAL_HANDLERS_REVISION_1;
184 hostSwitchHandler.Header.Revision = NDIS_SWITCH_OPTIONAL_HANDLERS_REVISION_1;
186 status = NdisFGetOptionalSwitchHandlers(ndisFilterHandle,
189 if (status != NDIS_STATUS_SUCCESS) {
190 OVS_LOG_ERROR("OvsExtAttach: Extension is running in "
191 "non-switch environment.");
192 OvsFreeMemoryWithTag(switchContext, OVS_SWITCH_POOL_TAG);
193 goto create_switch_done;
196 switchContext->NdisFilterHandle = ndisFilterHandle;
197 switchContext->NdisSwitchContext = hostSwitchContext;
198 RtlCopyMemory(&switchContext->NdisSwitchHandlers, &hostSwitchHandler,
199 sizeof(NDIS_SWITCH_OPTIONAL_HANDLERS));
201 status = OvsInitSwitchContext(switchContext);
202 if (status != NDIS_STATUS_SUCCESS) {
203 OvsFreeMemoryWithTag(switchContext, OVS_SWITCH_POOL_TAG);
204 goto create_switch_done;
207 status = OvsInitTunnelFilter(gOvsExtDriverObject, gOvsDeviceObject);
208 if (status != NDIS_STATUS_SUCCESS) {
209 OvsUninitSwitchContext(switchContext);
210 goto create_switch_done;
213 *switchContextOut = switchContext;
216 OVS_LOG_TRACE("Exit: switchContext: %p status: %#lx",
217 switchContext, status);
223 * --------------------------------------------------------------------------
224 * Implements filter driver's FilterDetach function.
225 * --------------------------------------------------------------------------
227 _Use_decl_annotations_
229 OvsExtDetach(NDIS_HANDLE filterModuleContext)
231 POVS_SWITCH_CONTEXT switchContext = (POVS_SWITCH_CONTEXT)filterModuleContext;
233 OVS_LOG_TRACE("Enter: filterModuleContext %p", filterModuleContext);
235 ASSERT(switchContext->dataFlowState == OvsSwitchPaused);
236 switchContext->controlFlowState = OvsSwitchDetached;
238 while(switchContext->pendingOidCount > 0) {
241 OvsDeleteSwitch(switchContext);
242 OvsCleanupIpHelper();
243 gOvsSwitchContext = NULL;
244 /* This completes the cleanup, and a new attach can be handled now. */
246 OVS_LOG_TRACE("Exit: OvsDetach Successfully");
251 * --------------------------------------------------------------------------
252 * This function deletes the switch by freeing all memory previously allocated.
253 * XXX need synchronization with other path.
254 * --------------------------------------------------------------------------
257 OvsDeleteSwitch(POVS_SWITCH_CONTEXT switchContext)
259 UINT32 dpNo = (UINT32) -1;
261 OVS_LOG_TRACE("Enter: switchContext:%p", switchContext);
265 dpNo = switchContext->dpNo;
266 OvsClearAllSwitchVports(switchContext);
267 OvsUninitTunnelFilter(gOvsExtDriverObject);
268 OvsUninitSwitchContext(switchContext);
270 OVS_LOG_TRACE("Exit: deleted switch %p dpNo: %d", switchContext, dpNo);
275 * --------------------------------------------------------------------------
276 * Implements filter driver's FilterRestart function.
277 * --------------------------------------------------------------------------
279 _Use_decl_annotations_
281 OvsExtRestart(NDIS_HANDLE filterModuleContext,
282 PNDIS_FILTER_RESTART_PARAMETERS filterRestartParameters)
284 POVS_SWITCH_CONTEXT switchContext = (POVS_SWITCH_CONTEXT)filterModuleContext;
285 NDIS_STATUS status = NDIS_STATUS_SUCCESS;
286 BOOLEAN switchActive;
288 UNREFERENCED_PARAMETER(filterRestartParameters);
290 OVS_LOG_TRACE("Enter: filterModuleContext %p",
291 filterModuleContext);
293 /* Activate the switch if this is the first restart. */
294 if (!switchContext->isActivated && !switchContext->isActivateFailed) {
295 status = OvsQuerySwitchActivationComplete(switchContext,
297 if (status != NDIS_STATUS_SUCCESS) {
298 switchContext->isActivateFailed = TRUE;
299 status = NDIS_STATUS_RESOURCES;
304 status = OvsActivateSwitch(switchContext);
306 if (status != NDIS_STATUS_SUCCESS) {
307 OVS_LOG_WARN("Failed to activate switch, dpNo:%d",
308 switchContext->dpNo);
309 status = NDIS_STATUS_RESOURCES;
315 ASSERT(switchContext->dataFlowState == OvsSwitchPaused);
316 switchContext->dataFlowState = OvsSwitchRunning;
319 OVS_LOG_TRACE("Exit: Restart switch:%p, dpNo: %d, status: %#x",
320 switchContext, switchContext->dpNo, status);
326 * --------------------------------------------------------------------------
327 * Implements filter driver's FilterPause function
328 * --------------------------------------------------------------------------
331 OvsExtPause(NDIS_HANDLE filterModuleContext,
332 PNDIS_FILTER_PAUSE_PARAMETERS pauseParameters)
334 POVS_SWITCH_CONTEXT switchContext = (POVS_SWITCH_CONTEXT)filterModuleContext;
336 UNREFERENCED_PARAMETER(pauseParameters);
337 OVS_LOG_TRACE("Enter: filterModuleContext %p",
338 filterModuleContext);
340 ASSERT(switchContext->dataFlowState == OvsSwitchRunning);
341 switchContext->dataFlowState = OvsSwitchPaused;
343 while(switchContext->pendingOidCount > 0) {
347 OVS_LOG_TRACE("Exit: OvsDetach Successfully");
348 return NDIS_STATUS_SUCCESS;
352 OvsInitSwitchContext(POVS_SWITCH_CONTEXT switchContext)
357 OVS_LOG_TRACE("Enter: switchContext: %p", switchContext);
359 switchContext->dispatchLock =
360 NdisAllocateRWLock(switchContext->NdisFilterHandle);
362 switchContext->portNoHashArray = (PLIST_ENTRY)OvsAllocateMemoryWithTag(
363 sizeof(LIST_ENTRY) * OVS_MAX_VPORT_ARRAY_SIZE, OVS_SWITCH_POOL_TAG);
364 switchContext->ovsPortNameHashArray = (PLIST_ENTRY)OvsAllocateMemoryWithTag(
365 sizeof(LIST_ENTRY) * OVS_MAX_VPORT_ARRAY_SIZE, OVS_SWITCH_POOL_TAG);
366 switchContext->portIdHashArray= (PLIST_ENTRY)OvsAllocateMemoryWithTag(
367 sizeof(LIST_ENTRY) * OVS_MAX_VPORT_ARRAY_SIZE, OVS_SWITCH_POOL_TAG);
368 switchContext->pidHashArray = (PLIST_ENTRY)OvsAllocateMemoryWithTag(
369 sizeof(LIST_ENTRY) * OVS_MAX_PID_ARRAY_SIZE, OVS_SWITCH_POOL_TAG);
370 switchContext->tunnelVportsArray = (PLIST_ENTRY)OvsAllocateMemoryWithTag(
371 sizeof(LIST_ENTRY) * OVS_MAX_VPORT_ARRAY_SIZE, OVS_SWITCH_POOL_TAG);
372 status = OvsAllocateFlowTable(&switchContext->datapath, switchContext);
374 if (status == NDIS_STATUS_SUCCESS) {
375 status = OvsInitBufferPool(switchContext);
377 if (status != NDIS_STATUS_SUCCESS ||
378 switchContext->dispatchLock == NULL ||
379 switchContext->portNoHashArray == NULL ||
380 switchContext->ovsPortNameHashArray == NULL ||
381 switchContext->portIdHashArray== NULL ||
382 switchContext->pidHashArray == NULL ||
383 switchContext->tunnelVportsArray == NULL) {
384 if (switchContext->dispatchLock) {
385 NdisFreeRWLock(switchContext->dispatchLock);
387 if (switchContext->portNoHashArray) {
388 OvsFreeMemoryWithTag(switchContext->portNoHashArray,
389 OVS_SWITCH_POOL_TAG);
391 if (switchContext->ovsPortNameHashArray) {
392 OvsFreeMemoryWithTag(switchContext->ovsPortNameHashArray,
393 OVS_SWITCH_POOL_TAG);
395 if (switchContext->portIdHashArray) {
396 OvsFreeMemoryWithTag(switchContext->portIdHashArray,
397 OVS_SWITCH_POOL_TAG);
399 if (switchContext->pidHashArray) {
400 OvsFreeMemoryWithTag(switchContext->pidHashArray,
401 OVS_SWITCH_POOL_TAG);
404 if (switchContext->tunnelVportsArray) {
405 OvsFreeMemory(switchContext->tunnelVportsArray);
408 OvsDeleteFlowTable(&switchContext->datapath);
409 OvsCleanupBufferPool(switchContext);
411 OVS_LOG_TRACE("Exit: Failed to init switchContext");
412 return NDIS_STATUS_RESOURCES;
415 for (i = 0; i < OVS_MAX_VPORT_ARRAY_SIZE; i++) {
416 InitializeListHead(&switchContext->ovsPortNameHashArray[i]);
417 InitializeListHead(&switchContext->portIdHashArray[i]);
418 InitializeListHead(&switchContext->portNoHashArray[i]);
419 InitializeListHead(&switchContext->tunnelVportsArray[i]);
422 for (i = 0; i < OVS_MAX_PID_ARRAY_SIZE; i++) {
423 InitializeListHead(&switchContext->pidHashArray[i]);
426 NdisAllocateSpinLock(&(switchContext->pidHashLock));
427 switchContext->isActivated = FALSE;
428 switchContext->isActivateFailed = FALSE;
429 switchContext->dpNo = OVS_DP_NUMBER;
430 ovsTimeIncrementPerTick = KeQueryTimeIncrement() / 10000;
432 OVS_LOG_TRACE("Exit: Succesfully initialized switchContext: %p",
434 return NDIS_STATUS_SUCCESS;
438 OvsUninitSwitchContext(POVS_SWITCH_CONTEXT switchContext)
440 OvsReleaseSwitchContext(switchContext);
444 * --------------------------------------------------------------------------
445 * Frees up the contents of and also the switch context.
446 * --------------------------------------------------------------------------
449 OvsDeleteSwitchContext(POVS_SWITCH_CONTEXT switchContext)
451 OVS_LOG_TRACE("Enter: Delete switchContext:%p", switchContext);
453 /* We need to do cleanup for tunnel port here. */
454 ASSERT(switchContext->numHvVports == 0);
455 ASSERT(switchContext->numNonHvVports == 0);
457 NdisFreeRWLock(switchContext->dispatchLock);
458 switchContext->dispatchLock = NULL;
459 NdisFreeSpinLock(&(switchContext->pidHashLock));
460 OvsFreeMemoryWithTag(switchContext->ovsPortNameHashArray,
461 OVS_SWITCH_POOL_TAG);
462 switchContext->ovsPortNameHashArray = NULL;
463 OvsFreeMemoryWithTag(switchContext->portIdHashArray,
464 OVS_SWITCH_POOL_TAG);
465 switchContext->portIdHashArray = NULL;
466 OvsFreeMemoryWithTag(switchContext->portNoHashArray,
467 OVS_SWITCH_POOL_TAG);
468 switchContext->portNoHashArray = NULL;
469 OvsFreeMemoryWithTag(switchContext->pidHashArray,
470 OVS_SWITCH_POOL_TAG);
471 switchContext->pidHashArray = NULL;
472 OvsFreeMemory(switchContext->tunnelVportsArray);
473 switchContext->tunnelVportsArray = NULL;
474 OvsDeleteFlowTable(&switchContext->datapath);
475 OvsCleanupBufferPool(switchContext);
477 OvsFreeMemoryWithTag(switchContext, OVS_SWITCH_POOL_TAG);
478 OVS_LOG_TRACE("Exit: Delete switchContext: %p", switchContext);
482 OvsReleaseSwitchContext(POVS_SWITCH_CONTEXT switchContext)
489 ref = gOvsSwitchContextRefCount;
490 newRef = (0 == ref) ? 0 : ref - 1;
491 icxRef = InterlockedCompareExchange(&gOvsSwitchContextRefCount,
494 } while (icxRef != ref);
497 OvsDeleteSwitchContext(switchContext);
502 OvsAcquireSwitchContext(VOID)
510 ref = gOvsSwitchContextRefCount;
511 newRef = (0 == ref) ? 0 : ref + 1;
512 icxRef = InterlockedCompareExchange(&gOvsSwitchContextRefCount,
515 } while (icxRef != ref);
525 * --------------------------------------------------------------------------
526 * This function activates the switch by initializing it with all the runtime
527 * state. First it queries all of the MAC addresses set as custom switch policy
528 * to allow sends from, and adds tme to the property list. Then it queries the
529 * NIC list and verifies it can support all of the NICs currently connected to
530 * the switch, and adds the NICs to the NIC list.
531 * --------------------------------------------------------------------------
534 OvsActivateSwitch(POVS_SWITCH_CONTEXT switchContext)
538 ASSERT(!switchContext->isActivated);
540 OVS_LOG_TRACE("Enter: activate switch %p, dpNo: %ld",
541 switchContext, switchContext->dpNo);
543 status = OvsAddConfiguredSwitchPorts(switchContext);
545 if (status != NDIS_STATUS_SUCCESS) {
546 OVS_LOG_WARN("Failed to add configured switch ports");
550 status = OvsInitConfiguredSwitchNics(switchContext);
552 if (status != NDIS_STATUS_SUCCESS) {
553 OVS_LOG_WARN("Failed to add configured vports");
554 OvsClearAllSwitchVports(switchContext);
557 switchContext->isActivated = TRUE;
558 OvsPostEvent(OVS_DEFAULT_PORT_NO, OVS_DEFAULT_EVENT_STATUS);
561 OVS_LOG_TRACE("Exit: activate switch:%p, isActivated: %s, status = %lx",
563 (switchContext->isActivated ? "TRUE" : "FALSE"), status);
569 * --------------------------------------------------------------------------
570 * Implements filter driver's FilterNetPnPEvent function.
571 * --------------------------------------------------------------------------
574 OvsExtNetPnPEvent(NDIS_HANDLE filterModuleContext,
575 PNET_PNP_EVENT_NOTIFICATION netPnPEvent)
577 NDIS_STATUS status = NDIS_STATUS_SUCCESS;
578 POVS_SWITCH_CONTEXT switchContext = (POVS_SWITCH_CONTEXT)filterModuleContext;
579 BOOLEAN switchActive;
581 OVS_LOG_TRACE("Enter: filterModuleContext: %p, NetEvent: %d",
582 filterModuleContext, (netPnPEvent->NetPnPEvent).NetEvent);
584 * The only interesting event is the NetEventSwitchActivate. It provides
585 * an asynchronous notification of the switch completing activation.
587 if (netPnPEvent->NetPnPEvent.NetEvent == NetEventSwitchActivate) {
588 status = OvsQuerySwitchActivationComplete(switchContext, &switchActive);
589 if (status != NDIS_STATUS_SUCCESS) {
590 switchContext->isActivateFailed = TRUE;
592 ASSERT(switchContext->isActivated == FALSE);
593 if (switchContext->isActivated == FALSE && switchActive == TRUE) {
594 status = OvsActivateSwitch(switchContext);
595 OVS_LOG_TRACE("OvsExtNetPnPEvent: activated switch: %p "
596 "status: %s", switchContext,
597 status ? "TRUE" : "FALSE");
602 if (status == NDIS_STATUS_SUCCESS) {
603 status = NdisFNetPnPEvent(switchContext->NdisFilterHandle,
606 OVS_LOG_TRACE("Exit: OvsExtNetPnPEvent");