@Override public void handleNiciraRole(OFMessage m) throws SwitchStateException { RoleState r = this.roleMan.extractNiciraRoleReply((OFExperimenter) m); if (r == null) { // The message wasn't really a Nicira role reply. We just // dispatch it to the OFMessage listeners in this case. this.handleMessage(m); return; } RoleRecvStatus rrs = this.roleMan.deliverRoleReply( new RoleReplyInfo(r, null, m.getXid())); if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { if (r == RoleState.MASTER) { this.transitionToMasterSwitch(); } else if (r == RoleState.EQUAL || r == RoleState.SLAVE) { this.transitionToEqualSwitch(); } } else { this.disconnectSwitch(); } }
/** * Send NX role request message to the switch requesting the specified * role. * * @param role role to request */ private int sendNxRoleRequest(RoleState role) throws IOException { // Convert the role enum to the appropriate role to send OFNiciraControllerRole roleToSend = OFNiciraControllerRole.ROLE_OTHER; switch (role) { case MASTER: roleToSend = OFNiciraControllerRole.ROLE_MASTER; break; case SLAVE: case EQUAL: default: // ensuring that the only two roles sent to 1.0 switches with // Nicira role support, are MASTER and SLAVE roleToSend = OFNiciraControllerRole.ROLE_OTHER; log.debug("Sending Nx Role.SLAVE to switch {}.", sw); } int xid = sw.getNextTransactionId(); OFExperimenter roleRequest = OFFactories.getFactory(OFVersion.OF_10) .buildNiciraControllerRoleRequest() .setXid(xid) .setRole(roleToSend) .build(); sw.sendRoleRequest(roleRequest); return xid; }
@Override public void handleNiciraRole(OFMessage m) throws SwitchStateException { RoleState r = this.roleMan.extractNiciraRoleReply((OFExperimenter) m); if (r == null) { // The message wasn't really a Nicira role reply. We just // dispatch it to the OFMessage listeners in this case. this.handleMessage(m); return; } RoleRecvStatus rrs = this.roleMan.deliverRoleReply( new RoleReplyInfo(r, null, m.getXid())); if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { if (r == RoleState.MASTER) { this.role = r; this.transitionToMasterSwitch(); } else if (r == RoleState.EQUAL || r == RoleState.SLAVE) { this.transitionToEqualSwitch(); } } else { this.disconnectSwitch(); } }
/** * Send NX role request message to the switch requesting the specified * role. * * @param role role to request */ private int sendNxRoleRequest(RoleState role) throws IOException { // Convert the role enum to the appropriate role to send OFNiciraControllerRole roleToSend = OFNiciraControllerRole.ROLE_OTHER; switch (role) { case MASTER: roleToSend = OFNiciraControllerRole.ROLE_MASTER; break; case SLAVE: case EQUAL: default: // ensuring that the only two roles sent to 1.0 switches with // Nicira role support, are MASTER and SLAVE roleToSend = OFNiciraControllerRole.ROLE_OTHER; log.warn("Sending Nx Role.SLAVE to switch {}.", sw); } int xid = sw.getNextTransactionId(); OFExperimenter roleRequest = OFFactories.getFactory(OFVersion.OF_10) .buildNiciraControllerRoleRequest() .setXid(xid) .setRole(roleToSend) .build(); sw.write(Collections.<OFMessage>singletonList(roleRequest)); return xid; }
/** * Send NX role request message to the switch requesting the specified * role. * * @param sw switch to send the role request message to * @param role role to request */ private int sendNxRoleRequest(Role role) throws IOException { // Convert the role enum to the appropriate role to send OFNiciraControllerRole roleToSend = OFNiciraControllerRole.ROLE_OTHER; switch (role) { case MASTER: roleToSend = OFNiciraControllerRole.ROLE_MASTER; break; case SLAVE: case EQUAL: default: // ensuring that the only two roles sent to 1.0 switches with // Nicira role support, are MASTER and SLAVE roleToSend = OFNiciraControllerRole.ROLE_SLAVE; log.warn("Sending Nx Role.SLAVE to switch {}.", sw); } int xid = sw.getNextTransactionId(); OFExperimenter roleRequest = factory10 .buildNiciraControllerRoleRequest() .setXid(xid) .setRole(roleToSend) .build(); sw.write(Collections.<OFMessage>singletonList(roleRequest), new FloodlightContext()); return xid; }
@Override void processOFExperimenter(OFChannelHandler h, OFExperimenter m) throws IOException, SwitchStateException { Role role = extractNiciraRoleReply(h, m); // If role == null it means the vendor (experimenter) message // wasn't really a Nicira role reply. We ignore this case. if (role != null) { RoleReplyInfo rri = new RoleReplyInfo(role, null, m.getXid()); RoleRecvStatus rrs = h.roleChanger.deliverRoleReply(rri); if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { setRoleAndStartDriverHandshake(h, rri.getRole()); } // else do nothing - wait for the correct expected reply } else { unhandledMessageReceived(h, m); } }
@Override void processOFExperimenter(OFChannelHandler h, OFExperimenter m) throws IOException, SwitchStateException { Role role = extractNiciraRoleReply(h, m); if (role == null) { // The message wasn't really a Nicira role reply. We just // dispatch it to the OFMessage listeners in this case. h.dispatchMessage(m); return; } RoleRecvStatus rrs = h.roleChanger.deliverRoleReply( new RoleReplyInfo(role, null, m.getXid())); if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { checkAndSetRoleTransition(h, role); } }
@Override void processOFExperimenter(OFChannelHandler h, OFExperimenter m) throws IOException, SwitchStateException { Role role = extractNiciraRoleReply(h, m); // If role == null it means the message wasn't really a // Nicira role reply. We ignore it in this state. if (role != null) { RoleRecvStatus rrs = h.roleChanger.deliverRoleReply( new RoleReplyInfo(role, null, m.getXid())); if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { checkAndSetRoleTransition(h, role); } } else { unhandledMessageReceived(h, m); } }
/** * Setup the mock switch and write capture for a role request, set the * role and verify mocks. * @param supportsNxRole whether the switch supports role request messages * to setup the attribute. This must be null (don't yet know if roles * supported: send to check) or true. * @param xid The xid to use in the role request * @param role The role to send * @throws IOException */ private void setupSwitchSendRoleRequestAndVerify(Boolean supportsNxRole, int xid, Role role) throws IOException { RoleRecvStatus expectation = RoleRecvStatus.MATCHED_SET_ROLE; expect(swImplBase.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(supportsNxRole).atLeastOnce(); if (supportsNxRole != null && supportsNxRole) { expect(swImplBase.getNextTransactionId()).andReturn(xid).once(); swImplBase.write(capture(writeCapture), EasyMock.<FloodlightContext>anyObject()); expectLastCall().anyTimes(); } replay(swImplBase); handler.sendRoleRequest(role, expectation); if (supportsNxRole != null && supportsNxRole) { List<OFMessage> msgs = getMessagesFromCapture(); assertEquals(1, msgs.size()); verifyNiciraMessage((OFExperimenter)msgs.get(0)); } }
/** * Extract the role from an OFVendor message. * * Extract the role from an OFVendor message if the message is a * Nicira role reply. Otherwise return null. * * @param experimenterMsg message * @return The role in the message if the message is a Nicira role * reply, null otherwise. * @throws SwitchStateException If the message is a Nicira role reply * but the numeric role value is unknown. */ @Override public RoleState extractNiciraRoleReply(OFExperimenter experimenterMsg) throws SwitchStateException { int vendor = (int) experimenterMsg.getExperimenter(); if (vendor != 0x2320) { return null; } OFNiciraControllerRoleReply nrr = (OFNiciraControllerRoleReply) experimenterMsg; RoleState role = null; OFNiciraControllerRole ncr = nrr.getRole(); switch (ncr) { case ROLE_MASTER: role = RoleState.MASTER; break; case ROLE_OTHER: role = RoleState.EQUAL; break; case ROLE_SLAVE: role = RoleState.SLAVE; break; default: //handled below } if (role == null) { String msg = String.format("Switch: [%s], " + "received NX_ROLE_REPLY with invalid role " + "value %s", sw.getStringId(), nrr.getRole()); throw new SwitchStateException(msg); } return role; }
@Override void processOFMessage(OFChannelHandler h, OFMessage m) throws IOException, SwitchStateException { if (h.sw.isDriverHandshakeComplete()) { moveToActive(h); h.state.processOFMessage(h, m); return; } if (m.getType() == OFType.ECHO_REQUEST) { processOFEchoRequest(h, (OFEchoRequest) m); } else if (m.getType() == OFType.ECHO_REPLY) { processOFEchoReply(h, (OFEchoReply) m); } else if (m.getType() == OFType.ROLE_REPLY) { h.sw.handleRole(m); } else if (m.getType() == OFType.ERROR) { if (!h.sw.handleRoleError((OFErrorMsg)m)) { h.sw.processDriverHandshakeMessage(m); if (h.sw.isDriverHandshakeComplete()) { moveToActive(h); } } } else { if (m.getType() == OFType.EXPERIMENTER && ((OFExperimenter) m).getExperimenter() == RoleManager.NICIRA_EXPERIMENTER) { h.sw.handleNiciraRole(m); } else { h.sw.processDriverHandshakeMessage(m); if (h.sw.isDriverHandshakeComplete()) { moveToActive(h); } } } }
void processOFExperimenter(OFChannelHandler h, OFExperimenter m) throws IOException, SwitchStateException { // TODO: it might make sense to parse the vendor message here // into the known vendor messages we support and then call more // specific event handlers unhandledMessageReceived(h, m); }
/** * Process an OF message received on the channel and * update state accordingly. * * The main "event" of the state machine. Process the received message, * send follow up message if required and update state if required. * * Switches on the message type and calls more specific event handlers * for each individual OF message type. If we receive a message that * is supposed to be sent from a controller to a switch we throw * a SwitchStateExeption. * * The more specific handlers can also throw SwitchStateExceptions * * @param h The OFChannelHandler that received the message * @param m The message we received. * @throws SwitchStateException * @throws IOException */ void processOFMessage(OFMessage m) { roleChanger.checkTimeout(); switch(m.getType()) { case BARRIER_REPLY: processOFBarrierReply((OFBarrierReply) m); break; case ERROR: processOFError((OFErrorMsg) m); break; case FLOW_REMOVED: processOFFlowRemoved((OFFlowRemoved) m); break; case GET_CONFIG_REPLY: processOFGetConfigReply((OFGetConfigReply) m); break; case PACKET_IN: processOFPacketIn((OFPacketIn) m); break; case PORT_STATUS: processOFPortStatus((OFPortStatus) m); break; case QUEUE_GET_CONFIG_REPLY: processOFQueueGetConfigReply((OFQueueGetConfigReply) m); break; case STATS_REPLY: processOFStatsReply((OFStatsReply) m); break; case ROLE_REPLY: processOFRoleReply((OFRoleReply) m); break; case EXPERIMENTER: processOFExperimenter((OFExperimenter) m); break; default: illegalMessageReceived(m); break; } }
@Override void processOFExperimenter(OFExperimenter m) { OFControllerRole role = extractNiciraRoleReply(m); // If role == null it measn the message wasn't really a // Nicira role reply. We ignore this case. if (role != null) { roleChanger.deliverRoleReply(m.getXid(), role); } else { unhandledMessageReceived(m); } }
@Override void processOFExperimenter(OFExperimenter m) { OFControllerRole role = extractNiciraRoleReply(m); // If role == null it means the message wasn't really a // Nicira role reply. We ignore just dispatch it to the // OFMessage listenersa in this case. if (role != null) { roleChanger.deliverRoleReply(m.getXid(), role); } else { dispatchMessage(m); } }
@Override void processOFExperimenter(OFExperimenter m) { OFControllerRole role = extractNiciraRoleReply(m); // If role == null it means the message wasn't really a // Nicira role reply. We ignore it. if (role != null) { roleChanger.deliverRoleReply(m.getXid(), role); } else { unhandledMessageReceived(m); } }