Changeset 251 for etherws/trunk
- Timestamp:
- 10/09/13 01:47:39 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r215 r251 372 372 373 373 374 class NetdevHandler(DebugMixIn): 375 IFTYPE = 'netdev' 376 READ_SIZE = 65535 377 ETH_P_ALL = 0x0003 # from <linux/if_ether.h> 378 379 def __init__(self, ioloop, switch, dev, debug=False): 380 self._ioloop = ioloop 381 self._switch = switch 382 self._dev = dev 383 self._debug = debug 384 self._sock = None 385 386 @property 387 def target(self): 388 return self._dev 389 390 @property 391 def closed(self): 392 return not self._sock 393 394 def open(self): 395 if not self.closed: 396 raise ValueError('Already opened') 397 self._sock = socket.socket( 398 socket.PF_PACKET, socket.SOCK_RAW, socket.htons(self.ETH_P_ALL)) 399 self._sock.bind((self._dev, self.ETH_P_ALL)) 400 self._ioloop.add_handler(self.fileno(), self, self._ioloop.READ) 401 return self._switch.register_port(self) 402 403 def close(self): 404 if self.closed: 405 raise ValueError('I/O operation on closed netdev') 406 self._switch.unregister_port(self) 407 self._ioloop.remove_handler(self.fileno()) 408 self._sock.close() 409 self._sock = None 410 411 def fileno(self): 412 if self.closed: 413 raise ValueError('I/O operation on closed netdev') 414 return self._sock.fileno() 415 416 def write_message(self, message, binary=False): 417 if self.closed: 418 raise ValueError('I/O operation on closed netdev') 419 self._sock.sendall(message) 420 421 def __call__(self, fd, events): 422 try: 423 self._switch.receive(self, EthernetFrame(self._read())) 424 return 425 except: 426 traceback.print_exc() 427 self.close() 428 429 def _read(self): 430 if self.closed: 431 raise ValueError('I/O operation on closed netdev') 432 buf = [] 433 while True: 434 buf.append(self._sock.recv(self.READ_SIZE)) 435 if len(buf[-1]) < self.READ_SIZE: 436 break 437 return ''.join(buf) 438 439 374 440 class TapHandler(DebugMixIn): 375 441 IFTYPE = 'tap' … … 514 580 websocket._SSLSocketWrapper = self._ssl 515 581 582 # XXX: may be blocked 516 583 try: 517 584 self._sock = websocket.WebSocket() … … 560 627 NAMESPACE = 'etherws.control' 561 628 IFTYPES = { 629 NetdevHandler.IFTYPE: NetdevHandler, 562 630 TapHandler.IFTYPE: TapHandler, 563 631 EtherWebSocketClient.IFTYPE: EtherWebSocketClient, … … 672 740 return {'entries': [self._ifstat(p) for p in self._switch.portlist 673 741 if isinstance(p.interface, TapHandler)]} 742 743 def _optparse_netdev(self, opt): 744 return {'debug': self._debug} 674 745 675 746 def _optparse_tap(self, opt): … … 1063 1134 iftype = parser_ctl_addport.add_subparsers(dest='iftype') 1064 1135 1136 # --- ctl addport netdev 1137 parser_ctl_addport_netdev = iftype.add_parser(NetdevHandler.IFTYPE, 1138 help='netdev') 1139 parser_ctl_addport_netdev.add_argument('target', 1140 help='device name to add interface') 1141 1065 1142 # --- ctl addport tap 1066 1143 parser_ctl_addport_tap = iftype.add_parser(TapHandler.IFTYPE,
Note: See TracChangeset
for help on using the changeset viewer.