Changeset 212
- Timestamp:
- 08/15/12 02:29:38 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r211 r212 45 45 import fcntl 46 46 import base64 47 import socket 47 48 import urllib2 48 49 import hashlib … … 391 392 def closed(self): 392 393 return not self._tap 394 395 @property 396 def address(self): 397 if self.closed: 398 raise ValueError('I/O operation on closed tap') 399 try: 400 return self._tap.addr 401 except: 402 return '' 403 404 @property 405 def netmask(self): 406 if self.closed: 407 raise ValueError('I/O operation on closed tap') 408 try: 409 return self._tap.netmask 410 except: 411 return '' 412 413 @property 414 def mtu(self): 415 if self.closed: 416 raise ValueError('I/O operation on closed tap') 417 return self._tap.mtu 418 419 @address.setter 420 def address(self, address): 421 if self.closed: 422 raise ValueError('I/O operation on closed tap') 423 self._tap.addr = address 424 425 @netmask.setter 426 def netmask(self, netmask): 427 if self.closed: 428 raise ValueError('I/O operation on closed tap') 429 self._tap.netmask = netmask 430 431 @mtu.setter 432 def mtu(self, mtu): 433 if self.closed: 434 raise ValueError('I/O operation on closed tap') 435 self._tap.mtu = mtu 393 436 394 437 def open(self): … … 609 652 return {'entries': [self._portstat(port)]} 610 653 654 def handle_setInterface(self, params): 655 portnum = int(params['port']) 656 port = self._switch.get_port(portnum) 657 address = params.get('address') 658 netmask = params.get('netmask') 659 mtu = params.get('mtu') 660 if not isinstance(port.interface, TapHandler): 661 raise ValueError('Port %d has unsupported interface: %s' % 662 (portnum, port.interface.IFTYPE)) 663 if address is not None: 664 port.interface.address = address 665 if netmask is not None: 666 port.interface.netmask = netmask 667 if mtu is not None: 668 port.interface.mtu = mtu 669 return {'entries': [self._ifstat(port)]} 670 671 def handle_listInterface(self, params): 672 return {'entries': [self._ifstat(p) for p in self._switch.portlist 673 if isinstance(p.interface, TapHandler)]} 674 611 675 def _optparse_tap(self, opt): 612 676 return {'debug': self._debug} … … 637 701 'rx': port.rx, 638 702 'shut': port.shut, 703 } 704 705 @staticmethod 706 def _ifstat(port): 707 return { 708 'port': port.number, 709 'type': port.interface.IFTYPE, 710 'target': port.interface.target, 711 'address': port.interface.address, 712 'netmask': port.interface.netmask, 713 'mtu': port.interface.mtu, 639 714 } 640 715 … … 813 888 print(fmt % 814 889 (r['port'], r['type'], shut, r['rx'], r['tx'], r['target'])) 890 891 def print_iflist(result): 892 pmax = maxlen(result, 'port', 4) 893 tmax = maxlen(result, 'type', 4) 894 amax = maxlen(result, 'address', 7) 895 nmax = maxlen(result, 'netmask', 7) 896 mmax = maxlen(result, 'mtu', 3) 897 fmt = ' %%%ds %%%ds %%%ds %%%ds %%%ds %%s' % \ 898 (pmax, tmax, amax, nmax, mmax) 899 print(fmt % ('Port', 'Type', 'Address', 'Netmask', 'MTU', 'Target')) 900 for r in result: 901 print(fmt % (r['port'], r['type'], 902 r['address'], r['netmask'], r['mtu'], r['target'])) 815 903 816 904 def handle_ctl_addport(args): … … 868 956 print_portlist(result['result']['entries']) 869 957 958 def handle_ctl_setif(args): 959 if args.port <= 0: 960 raise ValueError('Invalid port: %d' % args.port) 961 req = {'port': args.port} 962 address = getattr(args, 'address', None) 963 netmask = getattr(args, 'netmask', None) 964 mtu = getattr(args, 'mtu', None) 965 if address is not None: 966 if address: 967 socket.inet_aton(address) # validate 968 req['address'] = address 969 if netmask is not None: 970 if netmask: 971 socket.inet_aton(netmask) # validate 972 req['netmask'] = netmask 973 if mtu is not None: 974 if mtu < 576: 975 raise ValueError('Invalid MTU: %d' % mtu) 976 req['mtu'] = mtu 977 result = request(args, 'setInterface', req) 978 if 'error' in result: 979 _print_error(result['error']) 980 else: 981 print_iflist(result['result']['entries']) 982 983 def handle_ctl_listif(args): 984 result = request(args, 'listInterface') 985 if 'error' in result: 986 _print_error(result['error']) 987 else: 988 print_iflist(result['result']['entries']) 989 870 990 def handle_ctl_listfdb(args): 871 991 result = request(args, 'listFdb') … … 947 1067 parser_ctl_listport = control_method.add_parser('listport') 948 1068 1069 # -- ctl setif 1070 parser_ctl_setif = control_method.add_parser('setif') 1071 parser_ctl_setif.add_argument('port', type=int) 1072 parser_ctl_setif.add_argument('--address') 1073 parser_ctl_setif.add_argument('--netmask') 1074 parser_ctl_setif.add_argument('--mtu', type=int) 1075 1076 # -- ctl listif 1077 parser_ctl_listif = control_method.add_parser('listif') 1078 949 1079 # -- ctl listfdb 950 1080 parser_ctl_listfdb = control_method.add_parser('listfdb')
Note: See TracChangeset
for help on using the changeset viewer.