def udt_send(self, dst, data_S, priority=0): pkt = NetworkPacket(dst, data_S) print('%s: sending packet "%s" with priority %d' % (self, pkt, priority)) # encapsulate network packet in a link frame (usually would be done by the OS) fr = LinkFrame('Network', pkt.to_byte_S()) # enque frame onto the interface for transmission self.intf_L[0].put(fr.to_byte_S(), 'out')
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path #print("Label: " + m_fr.label) data = self.frwd_tbl_D.get(m_fr.label, '') #print("Data: " + data) if data != '': m_fr.label = data[0] m_fr.dst = data[1] interf = data[2] end = self.decap_tbl_D.get(m_fr.label, "CONTINUE") #print("END = " + end) print('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 try: if end != "CONTINUE": pkt = m_fr.to_Network_Packet() fr = LinkFrame('Network', pkt) else: fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[interf].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass else: print('****************Could not forward data********************') print('label was ' + str(m_fr))
def udt_send(self, dst, data_S, priority=0): pkt = NetworkPacket(dst, data_S) print('sending packet "%s"' % (str(pkt))) #encapsulate network packet in a link frame (usually would be done by the OS) fr = LinkFrame('Network', pkt.to_byte_S()) #enque frame onto the interface for transmission self.intf_L[0].put(fr.to_byte_S(), 'out') print("Placing %s on interface %s with mode %s\n" % (str(fr.to_byte_S()), str(0), str("out")))
def process_MPLS_frame(self, m_fr, i): # out MPLS label for packet out_label_S = None # new interface for forwarding out_intf_I = None # label for link layer out_link_label_S = None # string version of packet to be encapsolated by link layer out_pkt_S = None # get label from MPLS frame in_label_S = m_fr.label_S # get payload from MPLS frame in_payload_S = m_fr.data_S print('%s: processing MPLS frame "%s"' % (self, m_fr)) # get the key that we will be looking for in frwd_tbl_D and deap_tbl_D tbl_key = (in_label_S, i) # check to see if there is a rule for forwarding this packet if tbl_key in self.frwd_tbl_D.keys(): # get new MPLS label out_label_S = self.frwd_tbl_D[tbl_key][0] # get new interface to forward packet out_intf_I = self.frwd_tbl_D[tbl_key][1] # set link label as 'MPLS' out_link_label_S = 'MPLS' # create string payload for link layer to forward out_pkt_S = MPLSFrame(out_label_S, in_payload_S).to_byte_S() # else, check to see if there is a rule for decapsulation elif tbl_key in self.decap_tbl_D.keys(): # get new interface to forward packet out_intf_I = self.decap_tbl_D[tbl_key] # set link labe as 'Network' out_link_label_S = 'Network' # create string payload for link layer to forward out_pkt_S = in_payload_S # if nothing is defined, drop the packet else: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass # for now forward the frame out interface 1 try: fr = LinkFrame(out_link_label_S, out_pkt_S) self.intf_L[out_intf_I].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, out_intf_I)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass
def process_MPLS_frame(self, m_fr, i): # TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path #print('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 try: #checks if we need to decapsulate or not if i in self.decap_tbl_D: print("Decapsulating Block;\nRouter name:", str(self.name), "\nString received:", str(m_fr.to_byte_s())) else: fr = LinkFrame('MPLS', m_fr.to_byte_s()) self.intf_L[1].put(fr.to_byte_S(), 'out', True) # print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: # print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_queues(self): for i in range(len(self.intf_L)): fr_S = None #make sure we are starting the loop with a blank frame fr_S = self.intf_L[i].get('in') #get frame from interface i if fr_S is None: continue # no frame to process yet #decapsulate the packet fr = LinkFrame.from_byte_S(fr_S) pkt_S = fr.data_S #print("Packet_S when read in " + str(pkt_S)) #process the packet as network, or MPLS if fr.type_S == "Network": p = NetworkPacket.from_byte_S(pkt_S) #parse a packet out self.process_network_packet(p, i) elif fr.type_S == "MPLS": # TODO: handle MPLS frames m_fr = MPLSFrame.from_byte_S(pkt_S) #parse a frame out #for now, we just relabel the packet as an MPLS frame without encapsulation # m_fr = p #m_fr = MPLSFrame.from_byte_S(str(m_fr)) print("MPLS at beginning of handling " + str(m_fr)) #send the MPLS frame for processing self.process_MPLS_frame(m_fr, i) else: raise ('%s: unknown frame type: %s' % (self, fr.type))
def process_MPLS_frame(self, m_packet, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path #print('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 try: if i in self.decap_tbl_D: # decapsulate print("DECAPSULATION BLOCK\nRouter name = %s\nString received = %s\n" % (str(self.name),m_packet.to_byte_S())) else: fr = LinkFrame('MPLS', m_packet.to_byte_S()) self.intf_L[1].put(fr.to_byte_S(), 'out', True) #print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: #print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass
def udt_receive(self): fr_S = self.intf_L[0].get('in') if fr_S is None: return # decapsulate the network packet fr = LinkFrame.from_byte_S(fr_S) assert (fr.type_S == 'Network') # should be receiving network packets by hosts pkt_S = fr.data_S print('\n%s: received packet "%s"' % (self, pkt_S))
def process_MPLS_frame(self, m_fr, i): # TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 out_i = self.frwd_tbl_D[(i, m_fr.label)][0] m_fr.label = self.frwd_tbl_D[(i, m_fr.label)][1] type = 'MPLS' if m_fr.label in self.decap_tbl_D: type = 'Network' m_fr = m_fr.get_data() try: fr = LinkFrame(type, m_fr.to_byte_S()) self.intf_L[out_i].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): print('%s: processing MPLS frame "%s"' % (self, m_fr)) dst = m_fr.dst try: # Continue using MPLS if there is no need to decapsulate if self.decap_tbl_D[dst] is 0: fr = LinkFrame('MPLS', m_fr.to_byte_S()) else: fr = LinkFrame('Network', m_fr.to_byte_S()[m_fr.label_length:]) print('%s: decapsulated packet "%s" as Network frame "%s"' % (self, m_fr, fr)) # Send out on interface from the forwarding table self.intf_L[self.frwd_tbl_D[dst]].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, self.frwd_tbl_D[dst])) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('%s: processing MPLS frame "%s"' % (self, m_fr)) ## From the label received, we determine where it's going tbl_D = self.frwd_tbl_D[m_fr.label] m_fr.label = tbl_D["outLabel"] outInterface = tbl_D["intf"] ##see if we can decapsulate try: if m_fr.label == tbl_D['dest']: fr = LinkFrame("Network", m_fr.frame) else: fr = LinkFrame("MPLS", m_fr.to_byte_S()) # fr = LinkFrame('Network', m_fr.to_byte_S()) ##this is how it used to be set up. Always assume it was in there self.intf_L[outInterface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, outInterface)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): print('%s: processing MPLS frame "%s"' % (self, m_fr)) # get next destination based on the incoming label using table = self.frwd_tbl_D[m_fr.label] m_fr.label = table["outLabel"] outIntf = table["interface"] #if the queue is not full, try to decapsulate try: #if the next hop is the destination then we can decapsulate the mpls frame if m_fr.label == table['dest']: print('%s: decapsulating MPLS frame "%s"' % (self, m_fr)) fr = LinkFrame("Network", m_fr.frame) else: #otherwise forward as mpls frame fr = LinkFrame("MPLS", m_fr.to_byte_S()) self.intf_L[outIntf].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, outIntf)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): print('%s: processing MPLS frame "%s"' % (self, m_fr.__str__())) forward = self.frwd_tbl_D.get(i) if forward: pkt = m_fr fr = LinkFrame('MPLS', pkt.to_byte_S()) decap = self.decap_tbl_D.get(i) if decap: pkt = NetworkPacket.from_byte_S(m_fr.data_S.__str__()) print('%s: decapsulated packet "%s" from MPLS frame "%s"' % (self, pkt, m_fr)) fr = LinkFrame('Network', pkt.to_byte_S()) # for now forward the frame out interface 1 try: self.intf_L[1].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): print('%s: processing MPLS frame "%s"' % (self, m_fr)) ## From the label received, we determine where it's going inlabel = m_fr.label m_fr.label = self.frwd_tbl_D[inlabel][0] outInterface = self.frwd_tbl_D[inlabel][2] try: #decapsulate if m_fr.label == self.frwd_tbl_D[inlabel][1]: print("\nLAST HOP, DECAPSULATING\n") fr = LinkFrame("Network", m_fr.packet) else: #forward print("\nNOT LAST HOP, FORWARDING\n") fr = LinkFrame("MPLS", m_fr.to_byte_S()) self.intf_L[outInterface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, outInterface)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path if self.decap_tbl_D: #DL: MPLS decapsulation if there exists the destination host for the mpls package if self.decap_tbl_D[m_fr.dest]: packet = m_fr.packet else: #DL: MPLS forwarding if there's no decapsulation needed then it will be pushed on and relabeled for the next router for in_lbl in self.frwd_tbl_D: if m_fr.label == in_lbl: for interface in self.frwd_tbl_D[in_lbl]: m_fr.label = self.frwd_tbl_D[in_lbl][interface] print('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 try: #DL: If packet exists then we need to forward that network packet to the host if packet: try: fr = LinkFrame('Network', packet.to_byte_S()) self.intf_L[1].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass except: #DL: If that packet does not exist then we look to forward it along to the next router in the line try: fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[1].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass
def process_MPLS_frame(self, m_fr, i): interface = -1 if int(m_fr.label) in self.decap_tbl_D: interface = self.decap_tbl_D[int(m_fr.label)] packet = m_fr.pkt fr = LinkFrame('Network', packet.to_byte_S()) self.intf_L[interface].put(fr.to_byte_S(), 'out', True) print('%s: decapsulated frame "%s" from interface %d to %d' % (self, fr, i, 1)) else: check_tuple = (i, int(m_fr.label)) tuple = self.frwd_tbl_D[check_tuple] interface = tuple[0] m_fr.label = tuple[1] print('%s: processing MPLS frame "%s"' % (self, m_fr)) try: fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[interface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, fr, i)) pass
def process_MPLS_frame(self, m_fr, in_intf): print('\n%s: processing MPLS frame "%s"' % (self, m_fr)) # if the router has a decapsulation table with specific label if self.decap_tbl_D.get(m_fr.label) is not None: # Last Hop print("%s decapsulated packet." % self.name) # obtain network pack by decapsulation network_pkt = m_fr.packet in_label = m_fr.label intf_out = self.frwd_tbl_D[(in_intf, in_label)][0] try: fr = LinkFrame('Network', network_pkt) self.intf_L[intf_out].put(fr.to_byte_S(), 'out', True) print('\n%s: forwarding network packet "%s" from interface %d to %d' % (self, fr, intf_out, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, intf_out)) pass # if no decapsulation, then forward else: try: in_label = m_fr.label out_label = self.frwd_tbl_D[(in_intf, in_label)][1] intf_out = self.frwd_tbl_D[(in_intf, in_label)][0] m_fr.label = out_label fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[intf_out].put(fr.to_byte_S(), 'out', True) print('\n%s: forwarding frame "%s" from label %d to %d' % (self, fr, int(in_label), int(out_label))) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, intf_out)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print("*********** interface: ", self) print("*********** interface number: ", i) print('%s: processing MPLS frame "%s"' % (self, m_fr)) if self.decap_tbl_D.get( (str(self), i), "no" ) != "no": #check if the (interface, interface_number) is in the encap dict print( "* * * ** **** ** * * **** ** * ** ** * * * let's decapsulate" ) byte_s = m_fr.to_byte_S() pkt = NetworkPacket.from_byte_S(byte_s[3:]) print("THIS IS THE PACKET", pkt) fr = LinkFrame('Network', pkt.to_byte_S()) self.intf_L[1].put(fr.to_byte_S(), 'out', True) # for now forward the frame out interface 1 else: try: label = m_fr.get_Label() print(' %% % % % % % % % % % % % % % % % % %%', label) interface_n = self.frwd_tbl_D[str(self)][2] fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[interface_n].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('%s: processing MPLS frame "%s"' % (self, m_fr)) decap_interface = self.decap_tbl_D.get(m_fr.label) if decap_interface is not None: packet = m_fr.data_S out_interface = decap_interface try: fr = LinkFrame('Network', m_fr.data_S) self.intf_L[out_interface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass else: dictionary = self.frwd_tbl_D[(i, m_fr.label)] if dictionary is not None: out_interface = dictionary[0] try: fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[out_interface].put(fr.to_byte_S(), 'out', True) print( '%s: forwarding MPLS frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('%s: processing MPLS frame "%s"' % (self, m_fr)) if (m_fr.label == list(self.decap_tbl_D)[0]): # Last Hop print("Last Hop, Decapsulation") try: pkt = m_fr.dst + m_fr.data_S fr = LinkFrame('Network', pkt) self.intf_L[1].put(fr.to_byte_S(), 'out', True) print('%s: forwarding packet "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass else: print("Implement MPLS Forward") try: inlabel = m_fr.label outlabel = list( self.frwd_tbl_D[inlabel][i].keys())[0] #get out interface inter = self.frwd_tbl_D[inlabel][i][outlabel] #get out label m_fr.label = outlabel fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[inter].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from label %d to %d' % (self, fr, int(inlabel), int(outlabel))) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_network_packet(self, pkt, i): # create out label for packet and interface to send to out_label_S = None out_intf_I = None # check to see if interface is in encap_tbl_D if i in self.encap_tbl_D.keys(): out_label_S = self.encap_tbl_D[i][0] out_intf_I = self.encap_tbl_D[i][1] m_fr = MPLSFrame(out_label_S, pkt.to_byte_S()) print('%s: encapsulated packet "%s" as MPLS frame "%s"' % (self, pkt, m_fr)) #send the encapsulated packet for processing as MPLS frame # self.process_MPLS_frame(m_fr, out_inf_I) # send newly encapsulated MPLS frame try: fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[out_intf_I].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, out_intf_I)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, p, i)) pass
def process_network_packet(self, pkt, i): #TODO: encapsulate the packet in an MPLS frame based on self.encap_tbl_D #print("process_network_packet being called: ", self, pkt) print("*********** interface: ", self) print("*********** interface number: ", i) if self.encap_tbl_D.get( (str(self), i), "no" ) != "no": #check if the (interface, interface_number) is in the encap dict print("*#*#*#*#*#**#*#*#*#*##**#*# let's encapsulate") labelTuple = self.frwd_tbl_D[str(self)] label = labelTuple[0] + str(labelTuple[1]) m_fr = MPLSFrame(pkt, label) print('%s: encapsulated packet "%s" as MPLS frame "%s"' % (self, pkt, m_fr)) self.process_MPLS_frame( m_fr, i) #This will still get treated as a network packet initially else: print("is this the last network packet?") fr = LinkFrame('Network', pkt.to_byte_S()) self.intf_L[1].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1))
def process_queues(self): for i in range(len(self.intf_L)): fr_S = None #make sure we are starting the loop with a blank frame fr_S = self.intf_L[i].get('in') #get frame from interface i if fr_S is None: continue # no frame to process yet #decapsulate the packet fr = LinkFrame.from_byte_S(fr_S) pkt_S = fr.data_S #process the packet as network, or MPLS if fr.type_S == "Network": p = NetworkPacket.from_byte_S(pkt_S) #parse a packet out self.process_network_packet(p, i) elif fr.type_S == "MPLS": m_fr = MPLSFrame.from_byte_S(pkt_S) self.process_MPLS_frame(m_fr, i) else: raise ('%s: unknown frame type: %s' % (self, m_fr.type))
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path #we can do incoming interface, pkt_dst, and then outgoing interface: #decap_tbl_D = {0 : {H11 : 1}, 1: {H34: 3}, 2: {H17: 4}, 3: {H55: 0}} #better idea: just do pkt dst and outgoing interface {H1: 1} try: m_fr = MPLSFrame.from_byte_S(m_fr) except: pass #Get the ultimate destination out of the MPLS label print('%s: processing MPLS frame "%s"' % (self, m_fr)) m_fr_dst_tmp = re.search('(,\w{1,2},)', m_fr.label, flags=0).group() m_fr_dst = m_fr_dst_tmp.replace(',', '') #check to see if we have an interface to forward an MPLS out of if i in self.frwd_tbl_D: out_interface = self.frwd_tbl_D[i] frame_type = 'MPLS' try: fr = LinkFrame(frame_type, m_fr.to_byte_S()) self.intf_L[out_interface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, out_interface)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass elif m_fr_dst in self.decap_tbl_D: #No out interface for an MPLS #MPLS frame is meant to be decapsulated instead of forwarded out_interface = self.decap_tbl_D[m_fr_dst] frame_type = 'Network' pkt = NetworkPacket.from_byte_S(m_fr.packet) try: fr = LinkFrame(frame_type, pkt.to_byte_S()) self.intf_L[out_interface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, out_interface)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, pkt, i)) pass else: print('DEBUG: Something went wrong')
def process_queues(self): for i in range(len(self.intf_L)): fr_S = None #make sure we are starting the loop with a blank frame fr_S = self.intf_L[i].get('in') #get frame from interface i if fr_S is None: continue # no frame to process yet #decapsulate the packet fr = LinkFrame.from_byte_S(fr_S) pkt_S = fr.data_S #process the packet as network, or MPLS if fr.type_S == "Network": p = NetworkPacket.from_byte_S(pkt_S) #parse a packet out self.process_network_packet(p, i) elif fr.type_S == "MPLS": # TODO: handle MPLS frames # m_fr = MPLSFrame.from_byte_S(pkt_S) #parse a frame out #for now, we just relabel the packet as an MPLS frame without encapsulation # DEBUGGIN: DECAPSULATE MPLS # ===================================================================================================== # mplsFrame = MPLSFrame(pkt_S) mpls_frame = mplsFrame.from_byte_S(pkt_S) print( '\n\t\t @@@@=> DECAPSULATE MPLS --- PARSE A FRAME OUT <=@@@@\n' '\t\t @@@@=> NODE [%s] --- got MPLS Frame: %s on INTF [%d] <=@@@@\n' '\t\t @@@@=> NODE [%s] --- packet after decapsulation: %s <=@@@@\n' % (self.name, pkt_S, i, self.name, mpls_frame)) # ===================================================================================================== # #m_fr = p #m_fr = mpls_frame m_fr = mplsFrame #send the MPLS frame for processing self.process_MPLS_frame(m_fr, i) else: raise ('%s: unknown frame type: %s' % (self, fr.type))
def process_queues(self): for i in range(len(self.intf_L)): fr_S = None # make sure we are starting the loop with a blank frame fr_S = self.intf_L[i].get('in') # get frame from interface i if fr_S is None: continue # no frame to process yet # decapsulate the packet fr = LinkFrame.from_byte_S(fr_S) pkt_S = fr.data_S # process the packet as network, or MPLS if fr.type_S == "Network": p = NetworkPacket.from_byte_S(pkt_S) # parse a packet out print("Network queue: " + pkt_S) self.process_network_packet(p, i) elif fr.type_S == "MPLS": # TODO: handle MPLS frames print('MPLS QUEUE ' + pkt_S) # for now, we just relabel the packet as an MPLS frame without encapsulation # Parses a frame out mpls_packet = MPLS.from_byte_s(pkt_S) # send the MPLS frame for processing self.process_MPLS_frame(mpls_packet, i) else: raise('%s: unknown frame type: %s' % (self, fr.type))
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 outInterface = self.frwd_tbl_D[(m_fr.dst, i)][1] try: if self.decap_tbl_D[i] == m_fr.dst: fr = LinkFrame('Network', m_fr.data_S) self.intf_L[outInterface].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) else: fr = LinkFrame("MPLS", m_fr.to_byte_S()) self.intf_L[outInterface].put(fr.to_byte_S(), 'out', 'True') print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('%s: processing MPLS frame "%s"' % (self, m_fr)) try: if (self.decap_tbl_D[m_fr.dst] is "Y"): #if we are at an endpoint router, decapsulate pkt = NetworkPacket( m_fr.dst, m_fr.data_S) #Pull a network packet out of the MPLS frame fr = LinkFrame('Network', pkt.to_byte_S() ) #forward the network packet in a Link Frame self.intf_L[self.frwd_tbl_D[m_fr.dst]].put( fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) else: #we are not at an end router so encapsulate the MPLS Frame within a Link Frame fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[self.frwd_tbl_D[m_fr.dst]].put( fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_MPLS_frame(self, m_fr, i): #TODO: implement MPLS forward, or MPLS decapsulation if this is the last hop router for the path print('NODE [%s]: --- %s: processing MPLS frame "%s"' % (self.name, self, m_fr)) # for now forward the frame out interface 1 try: # Loop via forwarding table and compare node output interfaces to figure out when to decapsulate mpls frame for node_name, value1 in self.frwd_tbl_D.items(): if node_name is self.name: for out_inf, next_node_in_inf in value1.items(): # loop via decapsulate table to get interface at which we have to perform decapsulation # compare the decapsulation intarface from the decap_tbl_D # if its match then decapsulate and forward packet into host input intarface for decap_key, decap_value in self.decap_tbl_D.items(): # DECAPSULATE MPLS FRAME # RB is the last router in this topology, so we have to decapsulate mpls into network packet if self.name is 'RB': if decap_key is self.name and decap_value is i: pkt_s = str(m_fr) mplsFrame = MPLSFrame(pkt_s) mpls_frame_s = mplsFrame.from_byte_S(pkt_s) if pkt_s[0] is 'M': pkt_s = mpls_frame_s mplsFrame = MPLSFrame(mpls_frame_s) mpls_frame_s = mplsFrame.from_byte_S( pkt_s) print( '\n\t\t#####> LAST HOP ROUTER: DECAPSULATE MPLS --- PARSE A FRAME OUT <#####\n' '\t\t#####> NODE [%s] --- FRAME RECEIVED: %s <#####\n' '\t\t#####> NODE [%s] --- MPLS Frame: %s <#####\n' '\t\t#####> NODE [%s] --- Packet: %s <#####\n' % (self.name, pkt_s, self.name, pkt_s, self.name, mpls_frame_s)) # BUILD NEW NETWORK PACKET, AND FORWARD IT INTO HOTS p = NetworkPacket.from_byte_S( mpls_frame_s) # forward the frame out interface out_inf fr = LinkFrame('Network', p.to_byte_S()) self.intf_L[out_inf].put( fr.to_byte_S(), 'out', True) print( '%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) else: # forward the frame out interface out_inf fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[out_inf].put( fr.to_byte_S(), 'out', True) print( '%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) else: # forward the frame out interface out_inf fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[out_inf].put( fr.to_byte_S(), 'out', True) print( '%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, 1)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass