@@ -209,8 +209,8 @@ protected OvsTunnelNetworkVO createTunnelRecord(long from, long to, long network
209209 private void handleCreateTunnelAnswer (Answer [] answers ) {
210210 OvsCreateTunnelAnswer r = (OvsCreateTunnelAnswer )answers [0 ];
211211 String s =
212- String .format ("(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," + "greKey:%4$s, portName:%5$s)" , r . getFromIp (), r . getToIp (), r . getBridge (), r . getKey (),
213- r .getInPortName ());
212+ String .format ("(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," + "greKey:%4$s, portName:%5$s)" ,
213+ r .getFromIp (), r . getToIp (), r . getBridge (), r . getKey (), r . getInPortName ());
214214 Long from = r .getFrom ();
215215 Long to = r .getTo ();
216216 long networkId = r .getNetworkId ();
@@ -476,9 +476,13 @@ public void checkAndRemoveHostFromTunnelNetwork(Network nw, Host host) {
476476
477477 if (nw .getVpcId () != null && isVpcEnabledForDistributedRouter (nw .getVpcId ())) {
478478 List <Long > vmIds = _ovsNetworkToplogyGuru .getActiveVmsInVpcOnHost (nw .getVpcId (), host .getId ());
479+
479480 if (vmIds != null && !vmIds .isEmpty ()) {
480481 return ;
481482 }
483+
484+ // there are not active VM's on this host belonging to any of the tiers in the VPC, so remove
485+ // the host from the tunnel mesh network and destroy the bridge
482486 List <? extends Network > vpcNetworks = _vpcMgr .getVpcNetworks (nw .getVpcId ());
483487 try {
484488 for (Network network : vpcNetworks ) {
@@ -499,15 +503,16 @@ public void checkAndRemoveHostFromTunnelNetwork(Network nw, Host host) {
499503 }
500504 }
501505
502- Command cmd = new OvsDestroyBridgeCommand (nw .getId (), generateBridgeNameForVpc (nw .getVpcId ()));
506+ Command cmd = new OvsDestroyBridgeCommand (nw .getId (), generateBridgeNameForVpc (nw .getVpcId ()),
507+ host .getId ());
503508 s_logger .debug ("Destroying bridge for network " + nw .getId () + " on host:" + host .getId ());
504509 Answer ans = _agentMgr .send (host .getId (), cmd );
505510 handleDestroyBridgeAnswer (ans , host .getId (), nw .getId ());
506511 } catch (Exception e ) {
507512
508513 }
509514 } else {
510- List <Long > vmIds = _ovsNetworkToplogyGuru .getActiveVmsInNetworkOnHost (nw .getId (), host .getId ());
515+ List <Long > vmIds = _ovsNetworkToplogyGuru .getActiveVmsInNetworkOnHost (nw .getId (), host .getId (), true );
511516 if (vmIds != null && !vmIds .isEmpty ()) {
512517 return ;
513518 }
@@ -516,7 +521,7 @@ public void checkAndRemoveHostFromTunnelNetwork(Network nw, Host host) {
516521 * the tunnels for this network */
517522 int key = getGreKey (nw );
518523 String bridgeName = generateBridgeName (nw , key );
519- Command cmd = new OvsDestroyBridgeCommand (nw .getId (), bridgeName );
524+ Command cmd = new OvsDestroyBridgeCommand (nw .getId (), bridgeName , host . getId () );
520525 s_logger .debug ("Destroying bridge for network " + nw .getId () + " on host:" + host .getId ());
521526 Answer ans = _agentMgr .send (host .getId (), cmd );
522527 handleDestroyBridgeAnswer (ans , host .getId (), nw .getId ());
@@ -558,44 +563,66 @@ private String generateBridgeNameForVpc(long vpcId) {
558563 protected void checkAndCreateVpcTunnelNetworks (Host host , long vpcId ) {
559564
560565 long hostId = host .getId ();
561- List <? extends Network > vpcNetworks = _vpcMgr .getVpcNetworks (vpcId );
562- List <Long > vpcSpannedHostIds = _ovsNetworkToplogyGuru .getVpcSpannedHosts (vpcId );
563566 String bridgeName =generateBridgeNameForVpc (vpcId );
564- boolean bridgeNotSetup = true ;
565567
568+ List <Long > vmIds = _ovsNetworkToplogyGuru .getActiveVmsInVpcOnHost (vpcId , hostId );
569+
570+ if (vmIds == null || vmIds .isEmpty ()) {
571+
572+ // since this is the first VM from the VPC being launched on the host, first setup the bridge
573+ try {
574+ Commands cmds = new Commands (new OvsSetupBridgeCommand (bridgeName , hostId , null ));
575+ s_logger .debug ("Ask host " + hostId + " to create bridge for vpc " + vpcId + " and configure the "
576+ + " bridge for distributed routing." );
577+ Answer [] answers = _agentMgr .send (hostId , cmds );
578+ handleSetupBridgeAnswer (answers );
579+ } catch (OperationTimedoutException | AgentUnavailableException e ) {
580+ s_logger .warn ("Ovs Tunnel network created tunnel failed" , e );
581+ }
582+
583+ // now that bridge is setup, populate network acl's before the VM gets created
584+ OvsVpcRoutingPolicyConfigCommand cmd = prepareVpcRoutingPolicyUpdate (vpcId );
585+ cmd .setSequenceNumber (getNextRoutingPolicyUpdateSequenceNumber (vpcId ));
586+
587+ if (!sendVpcRoutingPolicyChangeUpdate (cmd , hostId , bridgeName )) {
588+ s_logger .debug ("Failed to send VPC routing policy change update to host : " + hostId +
589+ ". But moving on with sending the updates to the rest of the hosts." );
590+ }
591+ }
592+
593+ List <? extends Network > vpcNetworks = _vpcMgr .getVpcNetworks (vpcId );
594+ List <Long > vpcSpannedHostIds = _ovsNetworkToplogyGuru .getVpcSpannedHosts (vpcId );
566595 for (Network vpcNetwork : vpcNetworks ) {
567596 if (vpcNetwork .getState () != Network .State .Implemented &&
568597 vpcNetwork .getState () != Network .State .Implementing && vpcNetwork .getState () != Network .State .Setup )
569598 continue ;
599+
570600 int key = getGreKey (vpcNetwork );
571601 List <Long > toHostIds = new ArrayList <Long >();
572602 List <Long > fromHostIds = new ArrayList <Long >();
603+ OvsTunnelNetworkVO tunnelRecord = null ;
573604
574605 for (Long rh : vpcSpannedHostIds ) {
575606 if (rh == hostId ) {
576607 continue ;
577608 }
578- OvsTunnelNetworkVO ta = _tunnelNetworkDao .getByFromToNetwork (hostId , rh .longValue (), vpcNetwork .getId ());
579- // Try and create the tunnel even if a previous attempt failed
580- if (ta == null || ta .getState ().equals (OvsTunnel .State .Failed .name ())) {
609+ tunnelRecord = _tunnelNetworkDao .getByFromToNetwork (hostId , rh .longValue (), vpcNetwork .getId ());
610+ // Try and create the tunnel if does not exit or previous attempt failed
611+ if (tunnelRecord == null || tunnelRecord .getState ().equals (OvsTunnel .State .Failed .name ())) {
581612 s_logger .debug ("Attempting to create tunnel from:" + hostId + " to:" + rh .longValue ());
582- if (ta == null ) {
613+ if (tunnelRecord == null ) {
583614 createTunnelRecord (hostId , rh .longValue (), vpcNetwork .getId (), key );
584615 }
585616 if (!toHostIds .contains (rh )) {
586617 toHostIds .add (rh );
587618 }
588619 }
589-
590- ta = _tunnelNetworkDao .getByFromToNetwork (rh .longValue (),
591- hostId , vpcNetwork .getId ());
592- // Try and create the tunnel even if a previous attempt failed
593- if (ta == null || ta .getState ().equals (OvsTunnel .State .Failed .name ())) {
594- s_logger .debug ("Attempting to create tunnel from:" +
595- rh .longValue () + " to:" + hostId );
596- if (ta == null ) {
597- createTunnelRecord (rh .longValue (), hostId ,
598- vpcNetwork .getId (), key );
620+ tunnelRecord = _tunnelNetworkDao .getByFromToNetwork (rh .longValue (), hostId , vpcNetwork .getId ());
621+ // Try and create the tunnel if does not exit or previous attempt failed
622+ if (tunnelRecord == null || tunnelRecord .getState ().equals (OvsTunnel .State .Failed .name ())) {
623+ s_logger .debug ("Attempting to create tunnel from:" + rh .longValue () + " to:" + hostId );
624+ if (tunnelRecord == null ) {
625+ createTunnelRecord (rh .longValue (), hostId , vpcNetwork .getId (), key );
599626 }
600627 if (!fromHostIds .contains (rh )) {
601628 fromHostIds .add (rh );
@@ -615,20 +642,16 @@ protected void checkAndCreateVpcTunnelNetworks(Host host, long vpcId) {
615642 String otherIp = getGreEndpointIP (rHost , vpcNetwork );
616643 if (otherIp == null )
617644 throw new GreTunnelException (
618- "Unable to retrieve the remote "
619- + "endpoint for the GRE tunnel."
645+ "Unable to retrieve the remote endpoint for the GRE tunnel."
620646 + "Failure is on host:" + rHost .getId ());
621- Commands cmds = new Commands (
622- new OvsCreateTunnelCommand (otherIp , key ,
623- Long .valueOf (hostId ), i , vpcNetwork .getId (), myIp , bridgeName ,
624- vpcNetwork .getUuid ()));
647+ Commands cmds = new Commands ( new OvsCreateTunnelCommand (otherIp , key , Long .valueOf (hostId ),
648+ i , vpcNetwork .getId (), myIp , bridgeName , vpcNetwork .getUuid ()));
625649 s_logger .debug ("Attempting to create tunnel from:" + hostId + " to:" + i + " for the network "
626650 + vpcNetwork .getId ());
627651 s_logger .debug ("Ask host " + hostId
628652 + " to create gre tunnel to " + i );
629653 Answer [] answers = _agentMgr .send (hostId , cmds );
630654 handleCreateTunnelAnswer (answers );
631- bridgeNotSetup = false ;
632655 }
633656
634657 for (Long i : fromHostIds ) {
@@ -641,27 +664,12 @@ protected void checkAndCreateVpcTunnelNetworks(Host host, long vpcId) {
641664 + hostId );
642665 Answer [] answers = _agentMgr .send (i , cmds );
643666 handleCreateTunnelAnswer (answers );
644- bridgeNotSetup = false ;
645667 }
646668 } catch (GreTunnelException | OperationTimedoutException | AgentUnavailableException e ) {
647669 // I really thing we should do a better handling of these exceptions
648670 s_logger .warn ("Ovs Tunnel network created tunnel failed" , e );
649671 }
650672 }
651-
652- // If no tunnels have been configured, perform the bridge setup
653- // anyway. This will ensure VIF rules will be triggered
654- if (bridgeNotSetup ) {
655- try {
656- Commands cmds = new Commands (new OvsSetupBridgeCommand (bridgeName , hostId , null ));
657- s_logger .debug ("Ask host " + hostId + " to configure bridge for vpc" );
658- Answer [] answers = _agentMgr .send (hostId , cmds );
659- handleSetupBridgeAnswer (answers );
660- } catch (OperationTimedoutException | AgentUnavailableException e ) {
661- // I really thing we should do a better handling of these exceptions
662- s_logger .warn ("Ovs Tunnel network created tunnel failed" , e );
663- }
664- }
665673 }
666674
667675 @ Override
0 commit comments