Browse Source

cleanup unplugged ports

If an unplug RPC comes when agent or lagopus is down, port will
remain unplugged. Now a thread which detects unplug ports when
restarting agent or lagopus and performs unplug operation is added.

Change-Id: I07ef99a7ddde2f72d090c007d5bf9cd363cb4f8c
Itsuro Oda 1 year ago
parent
commit
984f2d510a
1 changed files with 34 additions and 0 deletions
  1. 34
    0
      networking_lagopus/ml2/agent/lagopus_agent.py

+ 34
- 0
networking_lagopus/ml2/agent/lagopus_agent.py View File

@@ -15,6 +15,7 @@ import os
15 15
 import socket
16 16
 import sys
17 17
 
18
+from neutron_lib.api.definitions import portbindings
18 19
 from neutron_lib import constants
19 20
 from neutron_lib import context
20 21
 from neutron_lib.utils import helpers
@@ -27,6 +28,7 @@ from osprofiler import profiler
27 28
 
28 29
 from neutron.agent.common import utils
29 30
 from neutron.agent.linux import ip_lib
31
+from neutron.agent.metadata import agent as meta_agent
30 32
 from neutron.agent import rpc as agent_rpc
31 33
 from neutron.api.rpc.callbacks import resources
32 34
 from neutron.common import config as common_config
@@ -88,6 +90,8 @@ class LagopusManager(object):
88 90
         self.bridge_mappings = bridge_mappings
89 91
         self.ryu_app = ryu_app
90 92
         self.serializer = eventlet.semaphore.Semaphore()
93
+        self.plugin_rpc = meta_agent.MetadataPluginAPI(topics.PLUGIN)
94
+        self.context = context.get_admin_context_without_session()
91 95
 
92 96
         self._wait_lagopus_initialized()
93 97
         lg_lib.register_config_change_callback(self._rebuild_dsl)
@@ -188,6 +192,9 @@ class LagopusManager(object):
188 192
                     vlan_id = bridge.dpid >> 48
189 193
                     phys_bridge.install_vlan(vlan_id, port2)
190 194
 
195
+        # start cleanup thread
196
+        eventlet.spawn_n(self.cleanup_unplugged_ports)
197
+
191 198
     def _wait_lagopus_initialized(self):
192 199
         for retry in range(MAX_WAIT_LAGOPUS_RETRY):
193 200
             try:
@@ -439,6 +446,33 @@ class LagopusManager(object):
439 446
         if not self.lagopus_alive:
440 447
             raise RuntimeError("lagopus is down.")
441 448
 
449
+    def _rawsock_exists(self, device, port_ids):
450
+        device = device[3:]  # remove 'tap' prefix
451
+        for pid in port_ids:
452
+            if pid.startswith(device):
453
+                return True
454
+
455
+    def cleanup_unplugged_ports(self):
456
+        LOG.debug("Cleanup thread start.")
457
+        filters = {portbindings.HOST_ID: [cfg.CONF.host]}
458
+        ports = self.plugin_rpc.get_ports(self.context, filters=filters)
459
+        port_ids = [port['id'] for port in ports]
460
+        LOG.debug("Ports on the host: %s", port_ids)
461
+
462
+        for port in self.ports.values():
463
+            i_type = port.interface.type
464
+            if i_type == lg_lib.INTERFACE_TYPE_VHOST:
465
+                if port.name not in port_ids:
466
+                    LOG.debug("Unplugged port %s detected.", port.name)
467
+                    self.unplug_vhost(None, port_id=port.name)
468
+            elif i_type == lg_lib.INTERFACE_TYPE_RAWSOCK:
469
+                device = port.interface.device
470
+                if not self._rawsock_exists(device, port_ids):
471
+                    LOG.debug("Unplugged port %s detected.", port.name)
472
+                    self.unplug_rawsock(None, device=device)
473
+
474
+        LOG.debug("Cleanup thread end.")
475
+
442 476
 
443 477
 class LagopusAgent(service.Service):
444 478
 

Loading…
Cancel
Save