2 * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netvirt.openstack.netvirt.impl;
11 import org.opendaylight.netvirt.openstack.netvirt.NetworkHandler;
12 import org.opendaylight.netvirt.openstack.netvirt.api.OvsdbTables;
13 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronNetwork;
14 import org.opendaylight.netvirt.openstack.netvirt.ConfigInterface;
15 import org.opendaylight.netvirt.openstack.netvirt.api.BridgeConfigurationManager;
16 import org.opendaylight.netvirt.openstack.netvirt.api.ConfigurationService;
17 import org.opendaylight.netvirt.openstack.netvirt.api.Constants;
18 import org.opendaylight.netvirt.openstack.netvirt.api.NetworkingProviderManager;
19 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
20 import org.opendaylight.netvirt.utils.config.ConfigProperties;
21 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
32 import java.net.InetAddress;
33 import java.net.NetworkInterface;
34 import java.net.UnknownHostException;
35 import java.util.ArrayList;
36 import java.util.Enumeration;
37 import java.util.List;
38 import java.util.Random;
40 import org.apache.commons.lang3.tuple.ImmutablePair;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * @author Madhu Venugopal
47 * @author Brent Salisbury
48 * @author Sam Hague (shague@redhat.com)
50 public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
51 private static final Logger LOG = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
53 // The implementation for each of these services is resolved by the OSGi Service Manager
54 private volatile ConfigurationService configurationService;
55 private volatile NetworkingProviderManager networkingProviderManager;
56 private volatile Southbound southbound;
57 private boolean intBridgeGenMac;
58 private Random random;
60 public BridgeConfigurationManagerImpl() {
64 public BridgeConfigurationManagerImpl(boolean intBridgeGenMac) {
65 this.intBridgeGenMac = intBridgeGenMac;
66 this.random = new Random(System.currentTimeMillis());
69 public void setConfigurationService(ConfigurationService configurationService) {
70 this.configurationService = configurationService;
73 public void setSouthbound(Southbound southbound) {
74 this.southbound = southbound;
78 public String getBridgeUuid(Node node, String bridgeName) {
79 return southbound.getBridgeUuid(node, bridgeName);
83 public boolean isNodeNeutronReady(Node node) {
84 Preconditions.checkNotNull(configurationService);
85 return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
89 public boolean isNodeOverlayReady(Node node) {
90 Preconditions.checkNotNull(configurationService);
91 return isNodeNeutronReady(node)
92 && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
96 public boolean isPortOnBridge (Node bridgeNode, String portName) {
97 return southbound.extractTerminationPointAugmentation(bridgeNode, portName) != null;
101 public boolean isNodeTunnelReady(Node bridgeNode, Node ovsdbNode) {
102 Preconditions.checkNotNull(configurationService);
103 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, configurationService.getIntegrationBridgeName())) {
104 LOG.trace("isNodeTunnelReady: node: {}, {} missing",
105 bridgeNode, configurationService.getIntegrationBridgeName());
109 return isNodeL3Ready(bridgeNode, ovsdbNode);
113 public boolean isNodeVlanReady(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
114 Preconditions.checkNotNull(configurationService);
116 final String brInt = configurationService.getIntegrationBridgeName();
117 if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, brInt)) {
118 LOG.trace("isNodeVlanReady: node: {}, {} missing", bridgeNode, brInt);
122 /* Check if physical device is added to br-int. */
123 String phyNetName = getPhysicalInterfaceName(ovsdbNode, network.getProviderPhysicalNetwork());
124 if (!isPortOnBridge(bridgeNode, phyNetName)) {
125 LOG.trace("isNodeVlanReady: node: {}, eth missing", bridgeNode);
129 return isNodeL3Ready(bridgeNode, ovsdbNode);
132 public boolean isNodeL3Ready(Node bridgeNode, Node ovsdbNode) {
133 Preconditions.checkNotNull(configurationService);
134 boolean ready = false;
135 if (configurationService.isL3ForwardingEnabled()) {
136 final String brInt = configurationService.getIntegrationBridgeName();
137 final String brExt = configurationService.getExternalBridgeName();
138 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
139 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
140 Preconditions.checkNotNull(portNameInt);
141 Preconditions.checkNotNull(portNameExt);
143 if (southbound.isBridgeOnOvsdbNode(ovsdbNode, brExt)) {
144 ready = isNetworkPatchCreated(bridgeNode, southbound.readBridgeNode(ovsdbNode, brExt));
146 LOG.trace("isNodeL3Ready: node: {}, {} missing",
156 public void prepareNode(Node ovsdbNode) {
157 Preconditions.checkNotNull(configurationService);
160 createIntegrationBridge(ovsdbNode);
161 } catch (Exception e) {
162 LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
167 if (configurationService.isL3ForwardingEnabled()) {
168 createExternalBridge(ovsdbNode);
170 } catch (Exception e) {
171 LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
174 // this node is an ovsdb node so it doesn't have a bridge
175 // so either look up the bridges or just wait for the bridge update to come in
176 // and add the flows there.
177 //networkingProviderManager.getProvider(node).initializeFlowRules(node);
181 * Check if the full network setup is available. If not, create it.
184 public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
185 boolean isCreated = false;
186 Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
187 if (ovsdbNode == null) {
188 //this should never happen
189 LOG.error("createLocalNetwork could not find ovsdbNode from bridge node {}", bridgeNode);
192 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
193 if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
195 isCreated = createBridges(bridgeNode, ovsdbNode, network);
196 } catch (Exception e) {
197 LOG.error("Error creating internal vlan net network {}--{}", bridgeNode, network, e);
202 } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
203 network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
204 if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
206 isCreated = createBridges(bridgeNode, ovsdbNode, network);
207 } catch (Exception e) {
208 LOG.error("Error creating internal vxlan/gre net network {}--{}", bridgeNode, network, e);
220 public String getExternalInterfaceName (Node node, String extNetwork) {
222 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
223 configurationService.getProviderMappingsKey());
224 if (providerMaps != null) {
225 for (String map : providerMaps.split(",")) {
226 String[] pair = map.split(":");
227 if (pair[0].equals(extNetwork)) {
234 LOG.error("External interface not found for Node: {}, Network {}",
238 LOG.info("External interface found for Node: {}, Network {} is {}",node,extNetwork,phyIf);
246 public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
248 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
249 configurationService.getProviderMappingsKey());
250 if (providerMaps == null) {
251 providerMaps = configurationService.getDefaultProviderMapping();
254 if (providerMaps != null) {
255 for (String map : providerMaps.split(",")) {
256 String[] pair = map.split(":");
257 if (pair[0].equals(physicalNetwork)) {
265 LOG.error("Physical interface not found for Node: {}, Network {}",
266 node, physicalNetwork);
273 public List<String> getAllPhysicalInterfaceNames(Node node) {
274 List<String> phyIfName = Lists.newArrayList();
275 String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
276 configurationService.getProviderMappingsKey());
277 if (providerMaps == null) {
278 providerMaps = configurationService.getDefaultProviderMapping();
281 if (providerMaps != null) {
282 for (String map : providerMaps.split(",")) {
283 String[] pair = map.split(":");
284 phyIfName.add(pair[1]);
292 * Returns true if a patch port exists between the Integration Bridge and Network Bridge
294 private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
295 Preconditions.checkNotNull(configurationService);
297 boolean isPatchCreated = false;
299 String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
300 if (isPortOnBridge(intBridge, portName)) {
301 portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
302 if (isPortOnBridge(netBridge, portName)) {
303 isPatchCreated = true;
307 return isPatchCreated;
311 * Creates the Integration Bridge
313 private boolean createIntegrationBridge(Node ovsdbNode) {
314 Preconditions.checkNotNull(configurationService);
316 if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName(),
317 intBridgeGenMac ? generateRandomMac() : null)) {
318 LOG.debug("Integration Bridge Creation failed");
324 private String generateRandomMac() {
325 byte[] macBytes = new byte[6];
326 random.nextBytes(macBytes);
327 macBytes[0] &= 0xfc; //the two low bits of the first byte need to be zero
329 StringBuilder stringBuilder = new StringBuilder();
333 stringBuilder.append(String.format("%02x", macBytes[i++]));
337 stringBuilder.append(':');
340 return stringBuilder.toString();
343 private boolean createExternalBridge(Node ovsdbNode) {
344 Preconditions.checkNotNull(configurationService);
346 if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName(), null)) {
347 LOG.debug("External Bridge Creation failed");
354 * Create and configure bridges for all network types and OpenFlow versions.
361 options: {peer=patch-int}
371 options: {peer=patch-net}
381 options: {peer=patch-int}
389 options: {peer=patch-net}
408 private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
409 Preconditions.checkNotNull(configurationService);
410 Preconditions.checkNotNull(networkingProviderManager);
412 LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
414 final String brInt = configurationService.getIntegrationBridgeName();
415 if (! createIntegrationBridge(ovsdbNode)) {
416 LOG.debug("{} Bridge creation failed", brInt);
420 if (configurationService.isL3ForwardingEnabled()) {
421 final String brExt = configurationService.getExternalBridgeName();
422 if (! createExternalBridge(ovsdbNode)) {
423 LOG.error("{} Bridge creation failed", brExt);
427 //get two patch port names
428 final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
429 final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
430 Preconditions.checkNotNull(portNameInt);
431 Preconditions.checkNotNull(portNameExt);
433 if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
434 LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
437 Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
438 Preconditions.checkNotNull(extBridgeNode, "br-ex cannot be null or empty!");
439 if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
440 LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
443 String extNetName = getExternalInterfaceName(extBridgeNode, brExt);
444 if ( extNetName != null) {
445 if (!addPortToBridge(extBridgeNode, brExt, extNetName)) {
446 LOG.error("Add External Port {} to Bridge {} failed", extNetName, brExt);
449 LOG.info("Add External Port {} to Ext Bridge {} success", extNetName, brExt);
452 /* For vlan network types add physical port to br-int. */
453 if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
454 String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
455 if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
456 LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
461 LOG.info("createBridges: node: {}, status: success", bridgeNode);
466 * Add a Port to a Bridge
468 private boolean addPortToBridge (Node node, String bridgeName, String portName) {
471 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
472 rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
475 LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
476 node.getNodeId().getValue(), bridgeName, portName);
478 LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
479 node.getNodeId().getValue(), bridgeName, portName);
482 LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
483 node.getNodeId().getValue(), bridgeName, portName);
490 * Add a Patch Port to a Bridge
492 private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
495 if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
496 rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
499 LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
500 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
502 LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
503 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
506 LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
507 node.getNodeId().getValue(), bridgeName, portName, peerPortName);
514 * Add Bridge to a Node
516 private boolean addBridge(Node ovsdbNode, String bridgeName, String mac) {
518 if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
519 (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
520 Class<? extends DatapathTypeBase> dpType = null;
521 if (configurationService.isUserSpaceEnabled()) {
522 dpType = DatapathTypeNetdev.class;
524 rv = southbound.addBridge(ovsdbNode, bridgeName, getControllersFromOvsdbNode(ovsdbNode), dpType, mac);
529 private String getControllerIPAddress() {
530 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
531 if (addressString != null) {
533 if (InetAddress.getByName(addressString) != null) {
534 return addressString;
536 } catch (UnknownHostException e) {
537 LOG.error("Host {} is invalid", addressString, e);
541 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
542 if (addressString != null) {
544 if (InetAddress.getByName(addressString) != null) {
545 return addressString;
547 } catch (UnknownHostException e) {
548 LOG.error("Host {} is invalid", addressString, e);
555 private short getControllerOFPort() {
556 short openFlowPort = Constants.OPENFLOW_PORT;
557 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
558 if (portString != null) {
560 openFlowPort = Short.parseShort(portString);
561 } catch (NumberFormatException e) {
562 LOG.warn("Invalid port:{}, use default({})", portString,
569 public List<String> getControllersFromOvsdbNode(Node node) {
570 List<String> controllersStr = new ArrayList<>();
572 String controllerIpStr = getControllerIPAddress();
573 if (controllerIpStr != null) {
574 // If codepath makes it here, the ip address to be used was explicitly provided.
575 // Being so, also fetch openflowPort provided via ConfigProperties.
576 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
577 + ":" + controllerIpStr + ":" + getControllerOFPort());
579 // Check if ovsdb node has manager entries
580 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
581 if (ovsdbNodeAugmentation != null) {
582 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
583 if (managerEntries != null && !managerEntries.isEmpty()) {
584 for (ManagerEntry managerEntry : managerEntries) {
585 if (managerEntry == null || managerEntry.getTarget() == null) {
588 String[] tokens = managerEntry.getTarget().getValue().split(":");
589 if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
590 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
591 + ":" + tokens[1] + ":" + getControllerOFPort());
592 } else if (tokens[0].equalsIgnoreCase("ptcp")) {
593 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
594 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
595 controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
596 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
597 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
599 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
602 LOG.trace("Skipping manager entry {} for node {}",
603 managerEntry.getTarget(), node.getNodeId().getValue());
607 LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
612 if (controllersStr.isEmpty()) {
613 // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
614 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
615 controllerIpStr = getLocalControllerHostIpAddress();
616 if (controllerIpStr != null) {
617 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
618 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
622 if (controllersStr.isEmpty()) {
623 LOG.warn("Failed to determine OpenFlow controller ip address");
624 } else if (LOG.isDebugEnabled()) {
625 controllerIpStr = "";
626 for (String currControllerIpStr : controllersStr) {
627 controllerIpStr += " " + currControllerIpStr;
629 LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
632 return controllersStr;
635 private String getLocalControllerHostIpAddress() {
636 String ipaddress = null;
638 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
639 NetworkInterface iface = ifaces.nextElement();
641 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
642 InetAddress inetAddr = inetAddrs.nextElement();
643 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
644 ipaddress = inetAddr.getHostAddress();
649 }catch (Exception e){
650 LOG.warn("Exception while fetching local host ip address ", e);
656 public void setDependencies(ServiceReference serviceReference) {
657 configurationService =
658 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
659 networkingProviderManager =
660 (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
662 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
666 public void setDependencies(Object impl) {