def __del__(self): """Clean up datapath """ output.dbg("Clean up datapath "+self.name, self.__class__.__name__) if (self.connected): self.disconnect() for i in self.interfaces[:]: self.del_if(i) cmd.run_cmd(DPCTL+" del-dp "+self.name, self.__class__.__name__)
def __init__(self, name): """Initialize datapath @param name name of datapath """ ##Name of datapath self.name = name cmd.run_cmd(DPCTL+" add-dp "+name, self.__class__.__name__) ##List of interfaces self.interfaces = [] ##Connected to controller or not self.connected = False
def get_wireless_info(self, intf): """Get wireless information of interface @param intf interface """ (ret, out) = cmd.run_cmd(IWCONFIG+" "+intf, self.__class__.__name__) return self.__parse_iwconfig("\n".join(out))
def up(self, intf): """Activate interface @param intf interface name """ return cmd.run_cmd(IFCONFIG+" "+intf+" up", self.__class__.__name__)
def down(self, intf): """Deactivate interface @param intf interface name """ return cmd.run_cmd(IFCONFIG+" "+intf+" down", self.__class__.__name__)
def disconnect(self): """Disconnect datapath from controller """ self.connected = False return cmd.run_cmd("screen -d -r coin-ovs-"+self.name+\ " -X quit", self.__class__.__name__)
def query_route(self): """Query for current route """ c = ROUTE+ " -n" (ret, out) = cmd.run_cmd(c, self.__class__.__name__) self.__routes = [] for l in out[2:]: self.__routes.append(route_entry(l))
def query_arp(self): """Query arp table """ c = ARP+ " -n" (ret, out) = cmd.run_cmd(c, self.__class__.__name__) self.__arp = [] for l in out[1:]: self.__arp.append(arp_entry(l))
def set_ip_mac(self, ip, mac): """Set mac address for IP address @param ip IP address @param mac Ethernet address """ c = ARP+" -s "+ip+" "+mac (ret, out) = cmd.run_cmd(c, self.__class__.__name__)
def add_if(self, intf): """Add interface to datapath @param intf name of interface @return command's exit status """ self.interfaces.append(intf) return cmd.run_cmd(DPCTL+" add-if "+self.name+" "+intf, self.__class__.__name__)
def get_ports(self): """Return dictionary of ports """ r = cmd.run_cmd(DPCTL+" show "+self.name, self.__class__.__name__) d = {} for l in r[1][2:]: pn = l.strip().split(":") d[pn[1].strip()] = int(pn[0][4:].strip()) return d
def set_eth_addr(self, intf, addr): """Set Ethernet addr @param intf interface name @param addr Ethernet address string """ self.down(intf) c = IFCONFIG+" "+intf+" hw ether "+addr ret = cmd.run_cmd(c, self.__class__.__name__) self.up(intf) return ret
def associate(self, intf, essid, ap=None, channel=None, rate=None, txpower = None): """Associate with an AP @param intf interface to associate AP with @param essid SSID to associate with @oaram ap mac address of AP @param channel channel to use @param rate rate to use """ c = IWCONFIG+" "+intf+" essid "+essid if channel != None: c += " channel "+str(channel) if ap != None: c += " ap "+ap if rate != None: c += " rate "+rate if txpower != None: c += " txpower "+txpower cmd.run_cmd(c, self.__class__.__name__)
def set_ipv4_addr(self, intf, addr, netmask=None): """Set IPv4 address @param intf interface name @param addr IPv4 address string @param netmask network mask string @return result of command """ c = IFCONFIG+" "+intf+" "+addr if (netmask != None): c += " netmask "+netmask return cmd.run_cmd(c, self.__class__.__name__)
def del_if(self, intf): """Remove interface to datapath @param intf name of interface @return command's exit status """ if (intf in self.interfaces): self.interfaces.remove(intf) return cmd.run_cmd(DPCTL+" del-if "+self.name+" "+intf, self.__class__.__name__) else: output.warn("Interface "+intf+" do not exist for deletion", self.__class__.__name__)
def get_wireless_scan(self, intf, ssid=None): """Scan wireless using interface @param intf interface """ (ret, out) = cmd.run_cmd(IWLIST+" "+intf+" scanning", self.__class__.__name__) scanr = self.__parsescan(out) if (ssid != None): scanrfiltered = [] for s in scanr: if (s["ESSID"] == ssid): scanrfiltered.append(s) return scanrfiltered return scanr
def change_route(self, command, target, gw=None, netmask=None, iface=None): """Change route @param cmd add or del @param target target, e.g., -net or -host or default @param gw gateway @param netmask network mask @param iface interface """ c = ROUTE+" "+command+" "+target if (gw != None): c += " gw "+gw if (netmask != None): c += " netmask "+netmask if (iface != None): c += " dev "+iface (ret, out) = cmd.run_cmd(c, self.__class__.__name__)
def veth_init_clean(self): """Initial veth cleanup """ for i in self.interfaces(): cmd.run_cmd("ip link del "+str(i), self.__class__.__name__)