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('%s: processing MPLS frame "%s"' % (self, m_fr)) # for now forward the frame out interface 1 try: print(len(self.frwd_tbl_D)) print(self.frwd_tbl_D.get(0)) i = 0 while i < len(self.frwd_tbl_D): print("In: %s out: %s, data %s" % (self.frwd_tbl_D.get(i)['in'], self.frwd_tbl_D.get(i)['out'], m_fr[:2])) if (self.frwd_tbl_D.get(i)['in'] == m_fr[:2] and self.frwd_tbl_D.get(i)['out'] == None): fr = LinkFrame( 'Network', MPLSFrame().decapsulate(m_fr, self.decap_tbl_D)) break i += 1 if (i == len(self.frwd_tbl_D)): fr = LinkFrame('MPLS', m_fr) 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): #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: fr = LinkFrame('Network', 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 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('%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)) if m_fr.label not in self.frwd_tbl_D: print('label destination not in forward table') return if m_fr.isLast == '1': ptype = "Network" else: ptype = "MPLS" pkt = m_fr.pkt o = self.frwd_tbl_D[m_fr.label] try: fr = LinkFrame(ptype, pkt) self.intf_L[o].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, o)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_network_packet(self, pkt, i): #TODO: encapsulate the packet in an MPLS frame based on self.encap_tbl_D #for now, we just relabel the packet as an MPLS frame without encapsulation #m_fr = pkt if i in self.encap_tbl_D.keys(): label = self.encap_tbl_D[i][0] intf = self.encap_tbl_D[i][1] m_fr = MPLSFrame(label, 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, i) ## copied from process MLPS frame right under try: fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[intf].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, intf)) 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.to_byte_S())) try: out_route = self.frwd_tbl_D.get(m_fr.lbl) outlbl = out_route[0] outinf = out_route[1] if outlbl in self.decap_tbl_D: #if the label matches a label end #decapsulate package outpkt = m_fr.pkt fr = LinkFrame('Network', outpkt) #send it out self.intf_L[outinf].put(fr.to_byte_S(), 'out', True) else: #forward the packet m_fr.switch_label(outlbl) fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[outinf].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, outinf)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass except KeyError: print("Key Error %d." (m_fr.lbl)) return except TypeError: print("Type Error.")
def process_MPLS_frame(self, m_fr, i): print('%s: processing MPLS frame "%s"' % (self, m_fr.to_byte_S())) try: info = self.decap_tbl_D[m_fr.label] pkt = m_fr.network_packet l_fr = LinkFrame('Network', pkt.to_byte_S()) out_intf = info[1] try: self.intf_L[out_intf].put(l_fr.to_byte_S(), 'out', True) print( '%s: forwarding Decapsulated NetPKT "%s" from interface %d to %d' % (self, pkt, i, out_intf)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass except KeyError: try: # Gets the info corresponding to the in_label and in interface info = self.frwd_tbl_D[m_fr.label][i] m_fr.label = info[0] out_intf = info[1] print("Assigning MPLS with new label %s" % info[0]) l_fr = LinkFrame('MPLS', m_fr.to_byte_S()) self.intf_L[out_intf].put(l_fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, m_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, priority): #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(self.decap_tbl_D[m_fr.dst] is "Y"): pkt = NetworkPacket(m_fr.dst, m_fr.data_S[1:], m_fr.data_S[0]) fr = LinkFrame('Network', pkt.to_byte_S()) #self.intf_L[self.frwd_tbl_D[m_fr.dst]].put(fr.to_byte_S(), 'out', True) j = self.frwd_tbl_D[i][priority][m_fr.dst] self.intf_L[j].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, j)) else: 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(i) #print(type(priority)) #print(m_fr.dst) j = self.frwd_tbl_D[i][priority][m_fr.dst] self.intf_L[j].put(fr.to_byte_S(), 'out', True) print('%s: forwarding frame "%s" from interface %d to %d' % (self, fr, i, j)) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, m_fr, i)) pass
def process_network_packet(self, pkt, i): #TODO: encapsulate the packet in an MPLS frame based on self.encap_tbl_D #for now, we just relabel the packet as an MPLS frame without encapsulation if pkt.dst in self.frwd_tbl_D: try: fr = LinkFrame('Network', pkt.to_byte_S()) self.intf_L[self.frwd_tbl_D[pkt.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[pkt.dst])) except queue.Full: print('%s: frame "%s" lost on interface %d' % (self, pkt, i)) pass m_fr = pkt last = 1 for hop in self.encap_tbl_D[pkt.dst]: m_fr = MPLSFrame(hop, m_fr, last) last = 0 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, i)
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 #send the MPLS frame for processing self.process_MPLS_frame(m_fr, i) else: raise ('%s: unknown frame type: %s' % (self, fr.type_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 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.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)) 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