/** * Create a new {@link EtherAddress} instance. * * @param mf A {@link MacAddressFilter} instance. * Note that MAC address mask is always ignored. * @return An {@link EtherAddress} instance if a valid MAC address is * configured in {@code mf}. Otherwise {@code null}. * @throws IllegalArgumentException * The given instance contains an invalid value. */ public static EtherAddress create(MacAddressFilter mf) { return (mf == null) ? null : create(mf.getAddress()); }
/** * Create an exception that indicates the same MAC address is configured * in the allowed host set. * * @param mac A MAC address. * @return A {@link RpcException} instance. */ public static RpcException getDuplicateMacAddressException(long mac) { String maddr = new EtherAddress(mac).getText(); return RpcException.getBadArgumentException( "Duplicate MAC address in allowed set: " + maddr); }
/** * Return the MAC address to be set as a {@link MacAddress} instance. * * @return A {@link MacAddress} instance. */ public final MacAddress getMacAddress() { return address.getMacAddress(); }
/** * Construct a new instance. * * @param mac A MAC address. * {@code null} is treated as undefined value. * @param vlan VLAN ID. Only lower 12 bits in the value is used. */ public MacVlan(MacAddress mac, int vlan) { EtherAddress eaddr = EtherAddress.create(mac); long addr = (eaddr == null) ? UNDEFINED : eaddr.getAddress(); encodedValue = encode(addr, vlan); }
/** * Ensure that the given MAC address is suitable for MAC mapping. * * @param eaddr An {@link EtherAddress} instance. * @throws RpcException * An invalid address is specified to {@code mac}. */ private void checkMacMap(EtherAddress eaddr) throws RpcException { String badaddr = null; if (eaddr.isBroadcast()) { badaddr = "Broadcast address"; } else if (!eaddr.isUnicast()) { badaddr = "Multicast address"; } else if (eaddr.getAddress() == UNDEFINED) { badaddr = "Zero MAC address"; } else { return; } throw RpcException.getBadArgumentException( badaddr + " cannot be specified: " + eaddr.getText()); }
/** * Create a new {@link EtherAddress} instance. * * @param mac A {@link MacAddress} instance. * @return An {@link EtherAddress} instance if {@code mac} is not * {@code null}. {@code null} if {@code mac} is {@code null}. * @throws IllegalArgumentException * The given instance contains an invalid value. */ public static EtherAddress create(MacAddress mac) { return (mac == null) ? null : new EtherAddress(mac); }
/** * Return a string representation of this object. * * @return A string representation of this object. */ @Override public String toString() { StringBuilder builder = new StringBuilder("EtherAddress["); return builder.append(getText()).append(']').toString(); } }
/** * Return a MD-SAL MAC address. * * @return A {@link MacAddress} instance. * Note that {@code null} is returned if no MAC address is * configured in this instance. */ public MacAddress getMacAddress() { long mac = getAddress(); if (mac == UNDEFINED) { return null; } EtherAddress eaddr = new EtherAddress(mac); return eaddr.getMacAddress(); }
/** * Construct a new instance. * * @param mac A byte array which represents a MAC address. * {@code null} and all-zeroed byte array are treated as * undefined value. * @param vlan VLAN ID. Only lower 12 bits in the value is used. */ public MacVlan(byte[] mac, int vlan) { this((mac == null) ? UNDEFINED : EtherAddress.toLong(mac), vlan); }
/** * Search for a MAC address table entry specified by the given MAC address * in the MAC address table. * * @param ctx MD-SAL datastore transaction context. * @param eaddr An {@link EtherAddress} instance that specifies the * MAC address. * @return A {@link MacEntry} instance if found. * {@code null} if not found. * @throws VTNException An error occurred. */ private MacEntry getMacEntry(TxContext ctx, EtherAddress eaddr) throws VTNException { // Multicast address should never be found in the MAC address table. MacEntry ment = null; if (eaddr.isUnicast()) { BridgeIdentifier<Vbridge> vbrId = getIdentifier(); ReadWriteTransaction tx = ctx.getReadWriteTransaction(); InstanceIdentifier<MacTableEntry> path = getMacEntryPath(vbrId, eaddr.getMacAddress()); Optional<MacTableEntry> opt = DataStoreUtils.read(tx, path); if (opt.isPresent()) { ment = new CurrentMacEntry(eaddr, opt.get()); } } return ment; }
/** * Initialize this instance using the given string. * * @param value A string which represents a pair of MAC address and a * VLAN ID. The given string must consist of a MAC address * and VLAN ID (decimal) joined with "@". * An empty MAC address is treated as MAC address is not * specified. * @throws RpcException * {@code parser} contains invalid value. */ private void initialize(String value) throws RpcException { VlanDescParser parser = new VlanDescParser(value, DESC_VLAN_HOST_DESC); String mac = parser.getIdentifier(); long addr; if (mac == null) { addr = UNDEFINED; } else { try { EtherAddress eaddr = new EtherAddress(mac); checkMacMap(eaddr); addr = eaddr.getAddress(); } catch (RuntimeException e) { throw RpcException.getBadArgumentException( "Invalid MAC address in vlan-host-desc: " + value, e); } } stringCache = MiscUtils.toLowerCase(value); encodedValue = encode(addr, parser.getVlanId()); }
/** * Determine whether the given Ethernet header matches the condition * for MAC address configured in this instance. * * @param ctx A {@link FlowMatchContext} instance. * @param eth An {@link EtherHeader} instance. * @return {@code true} only if the given Ethernet header matches all * the conditions for MAC address configured in this instance. */ private boolean matchAddress(FlowMatchContext ctx, EtherHeader eth) { if (sourceAddress != null) { ctx.addMatchField(FlowMatchType.DL_SRC); if (sourceAddress.getAddress() != eth.getSourceAddress().getAddress()) { return false; } } // Check the destination MAC address. if (destinationAddress != null) { ctx.addMatchField(FlowMatchType.DL_DST); if (destinationAddress.getAddress() != eth.getDestinationAddress().getAddress()) { return false; } } return true; }
/** * Determine whether the given MAC address should be learned in the * MAC address table or not. * * @param pctx A runtime context for a received packet. * @param eaddr An {@link EtherAddress} to be tested. * @return {@code true} only if the given MAC address should be learned * in the MAC address. */ private boolean isMacAddressValid(PacketContext pctx, EtherAddress eaddr) { // Multicast address should be ignored. boolean valid = eaddr.isUnicast(); if (valid) { valid = (eaddr.getAddress() != 0L); if (!valid) { pctx.getTxContext(). log(LOG, VTNLogLevel.WARN, "{}: Ignore zero MAC address: {}", getIdentifier(), pctx.getFrame()); } } return valid; }
byte[] sha = src.getBytes(); byte[] dstMac = dst.getBytes(); byte[] tha = (dst.isBroadcast()) ? new byte[EtherAddress.SIZE] : dstMac; Ip4Network sender = senderProtocolAddress;
/** * Invoked when a packet has been received. * * @param ev A {@link PacketInEvent} instance. */ @Override public void notifyPacket(PacketInEvent ev) { PacketContext pctx = new PacketContext(ev); EtherAddress src = pctx.getSourceAddress(); if (src.equals(pctx.getControllerAddress())) { if (LOG.isTraceEnabled()) { LOG.trace("Ignore self-originated packet: {}", pctx.getDescription()); } } else { try { receive(pctx, src); } catch (VTNException | RuntimeException e) { TxContext ctx = ev.getTxContext(); ctx.log(LOG, VTNLogLevel.ERROR, e, "Ignore received packet due to error: %s", pctx.getDescription()); } } }
/** * Convert the given byte array that represents an ethernet address into * a long integer number. * * @param b A byte array. * @return A long integer number. * @throws NullPointerException * {@code b} is {@code null}. * @throws IllegalArgumentException * The length of {@code b} is not 6. */ public static long toLong(byte[] b) { checkAddress(b); long num = 0L; int i = 0; do { num <<= Byte.SIZE; num |= b[i] & NumberUtils.MASK_BYTE; i++; } while (i < SIZE); return num; }
/** * {@inheritDoc} */ @Override protected final void verifyImpl() throws RpcException { if (address == null) { String msg = getErrorMessage("MAC address"); throw RpcException.getNullArgumentException(msg); } if (address.isBroadcast()) { String msg = getErrorMessage( "Broadcast address cannot be specified."); throw RpcException.getBadArgumentException(msg); } if (!address.isUnicast()) { String msg = getErrorMessage( "Multicast address cannot be specified", address.getText()); throw RpcException.getBadArgumentException(msg); } if (address.getAddress() == 0L) { String msg = getErrorMessage("Zero cannot be specified."); throw RpcException.getBadArgumentException(msg); } }
/** * Create a new {@link EtherAddress} instance. * * @param bytes A byte array which represents an ethernet address. * @return An {@link EtherAddress} instance if {@code bytes} is not * {@code null}. {@code null} if {@code bytes} is {@code null}. * @throws IllegalArgumentException * The length of {@code bytes} is not 6. */ public static EtherAddress create(byte[] bytes) { return (bytes == null) ? null : new EtherAddress(bytes); }