ccie18643 / PyTCP

PyTCP is a fully functional TCP/IP stack written in Python. It supports TCP stream-based transport with reliable packet delivery based on a sliding window mechanism and basic congestion control. It also supports IPv6/ICMPv6 protocols with SLAAC address configuration. It operates as a user space program attached to the Linux TAP interface.
GNU General Public License v3.0
349 stars 35 forks source link

Configurable Tx/Rx ring buffer threads #31

Open mikeawalker opened 5 months ago

mikeawalker commented 5 months ago

Loving this library....really useful for some network simulation and learning I am doing.

For my use case I dont want to use a tap device. Instead I just want to intercept the ethernet frames and 'transport' them via my own means. I can do this as is with the following hack


class QueueTxRing( pytcp.subsystems.tx_ring.TxRing ):
    def __init__( self ):
        self.queue = queue.Queue( maxsize = 10000 )
        super().__init__( )
    def start( self , fd ):
        self._run_thread = True
        threading.Thread(target=self.__thread_transmit).start()
        time.sleep(0.1)
    def get( self ):
        return self.queue.get( )
    def __thread_transmit( self ):
        log.info("IP Stack using overwritten tx thread - yay!")
        MAX_LEN = config.TAP_MTU 
        frame_buffer = bytearray( MAX_LEN )

        while self._run_thread:
            self._packet_enqueued.acquire(timeout=0.1)
            if not self._tx_ring:
                continue

            packet_tx = self._tx_ring.pop(0) 
            packet_tx_len = len( packet_tx )
            if packet_tx_len > MAX_LEN: 
                continue 
            frame = memoryview( frame_buffer)[:packet_tx_len ]
            packet_tx.assemble( frame )
            try: 
                self.queue.put( frame )
                # write to queue 
                pass 
            except:
                log.error("fail ethernet tx") 
                continue 
        log.debug("Tx ring stopped")

stack.tx_ring = QueueTxRing( ) 

I'd be interested in submitting a formal feature where the use of a tap (or otherwise) is configurable at run time. If this is something you'd except let me know and I can create a merge request

ccie18643 commented 1 month ago

Sure please create merge request. Thank you.