def __init__(self, xml_network_number): self.gm = GraphMan() if is_scheduler_run: self.xml_parser = XMLParser("net_xmls/net_2p_stwithsingleitr.xml", str(xml_network_number)) else: self.xml_parser = XMLParser( "ext/net_xmls/net_2p_stwithsingleitr.xml", str(xml_network_number)) self.init_network_from_xml() #Useful state variables self.last_sch_req_id_given = -1 self.last_tp_dst_given = info_dict['base_sport'] - 1 #Scher state dicts self.N = 0 #num_activesessions self.sessions_beingserved_dict = {} self.sessions_pre_served_dict = {} self.sid_res_dict = {} self.actual_res_dict = self.gm.give_actual_resource_dict() #for perf plotting self.perf_plotter = PerfPlotter(self.actual_res_dict) #for control_comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag='scher-acter', proto='tcp', _recv_callback=self._handle_recvfromacter, s_addr=info_dict['lacter_addr'], c_addr=info_dict['acterl_addr'])
def __init__(self, intf, pl_port, dtsl_ip, dtsl_port, cl_ip, proto, tx_type, file_url, req_dict,app_pref_dict, htbdir): self.intf = intf self.pl_ip = get_addr(intf) self.pl_port = pl_port self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port self.cl_ip = cl_ip self.proto = proto self.tx_type = tx_type self.file_url = file_url self.req_dict = req_dict self.app_pref_dict = app_pref_dict self.htbdir = htbdir #for control state '''0:start, 1:joined to dts, 2:sch_reply is recved''' self.state = 0 #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'p-dts', proto = 'udp', _recv_callback = self._handle_recvfromdts, s_addr = (self.pl_ip,self.pl_port), c_addr = (self.dtsl_ip,self.dtsl_port) ) #for htb_queue conf - bw shaping self.ktif = 230 #kernel timer interrupt frequency (Hz) self.init_htbdir()
def __init__(self, xml_net_num, sching_logto): logging.basicConfig(level=logging.DEBUG) self.sching_logto = sching_logto if is_scheduler_run: self.xml_parser = XMLParser("net_2p_stwithsingleitr.xml", str(xml_net_num)) else: self.xml_parser = XMLParser("ext/net_2p_stwithsingleitr.xml", str(xml_net_num)) self.gm = GraphMan() self.init_network_from_xml() #Useful state variables self.last_sching_id_given = -1 self.last_sch_req_id_given = -1 self.last_tp_dst_given = info_dict['base_sport']-1 #Scher state dicts self.num_dstusers = 0 self.users_beingserved_dict = {} #user_ip:{'gw_dpid':<>,'gw_conn_port':<> ...} # self.N = 0 #num_activesessions self.alloc_dict = None self.sessions_beingserved_dict = {} self.sessions_pre_served_dict = {} self.sid_res_dict = {} self.actual_res_dict = self.gm.give_actual_resource_dict() #for perf plotting self.perf_plotter = PerfPlotter(self.actual_res_dict) #for control_comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'scher-acter', proto = 'tcp', _recv_callback = self._handle_recvfromacter, s_addr = info_dict['lacter_addr'], c_addr = info_dict['acterl_addr'] )
def __init__ (self): #TODO: active sching_realization #for control comm with scher, ... self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'acter-scher', proto = 'tcp', _recv_callback = self._handle_recvfromscher, s_addr = info_dict['lscher_addr'], c_addr = info_dict['scherl_addr'] ) #core.addListeners(self) <- bu aptal neden calismiyo anlamadim core.openflow.addListenerByName("ConnectionUp", self._handle_ConnectionUp) core.openflow.addListenerByName("FlowStatsReceived", self._handle_FlowStatsReceived) core.openflow.addListenerByName("PacketIn", self._handle_PacketIn )
def __init__(self, xml_net_num, sching_logto, data_over_tp, act): # logging.basicConfig(filename='logs/schinglog',filemode='w',level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG) # if not (sching_logto == 'console' or sching_logto == 'file'): logging.error('Unexpected sching_logto=%s', sching_logto) return self.sching_logto = sching_logto # if not (data_over_tp == 'tcp' or data_over_tp == 'udp'): logging.error('Unexpected data_over_tp=%s', data_over_tp) return self.data_over_tp = data_over_tp # self.net_xml_file_url = "net_xmls/net_simpler.xml" #"net_xmls/net_four_paths.xml" #"net_xmls/net_mesh_topo.xml" #"net_xmls/net_resubmit_exp.xml" if not is_scheduler_run: self.net_xml_file_url = "ext/" + self.net_xml_file_url logging.info("Scheduler:: self.net_xml_file_url= %s", self.net_xml_file_url) self.xml_parser = XMLParser(self.net_xml_file_url, str(xml_net_num)) # self.gm = GraphMan() self.init_network_from_xml() # self.gm.print_graph() # Useful state variables self.last_sching_id_given = -1 self.last_sch_req_id_given = -1 self.last_tp_dst_given = info_dict['base_sport']-1 # Scher state dicts self.num_dstusers = 0 self.users_beingserved_dict = {} # user_ip:{'gw_dpid':<>, 'gw_conn_port':<> ...} # self.N = 0 #num_activesessions self.alloc_dict = None self.sessionsbeingserved_dict = {} self.sessionspreserved_dict = {} self.sid_res_dict = {} self.actual_res_dict = self.gm.get_actual_resource_dict() # for perf plotting # self.perf_plotter = PerfPlotter(self.actual_res_dict) # for control_comm self.cci = ControlCommIntf() self.act = act if act: self.cci.reg_commpair(sctag = 'scher-acter', proto = 'tcp', _recv_callback = self._handle_recvfromacter, s_addr = info_dict['lacter_addr'], c_addr = info_dict['acterl_addr'] ) self.dtsuser_intf = DTSUserCommIntf() # self.couplinginfo_dict = {} self.starting_time = time.time() # self.sid_schregid_dict = {} self.schregid_sid_dict = {} self.schingid_rescapalloc_dict = {} self.geninfo_dict = {}
def __init__(self, cl_ip, cl_port_list, dtsl_ip, dtsl_port, dtst_port, proto, rx_type): self.cl_ip = cl_ip self.cl_port_list = cl_port_list self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port self.dtst_port = dtst_port self.proto = proto self.rx_type = rx_type #for control state '''0:start, 1:joined to dts, 2:ready to recv s_data''' self.state = 0 #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag='c-dts', proto='udp', _recv_callback=self._handle_recvfromdts, s_addr=(self.cl_ip, self.dtst_port), c_addr=(self.dtsl_ip, self.dtsl_port))
def __init__ (self): #TODO: active sching_realization #for control comm with scher, ... self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'acter-scher', proto = 'tcp', _recv_callback = self._handle_recvfromscher, s_addr = info_dict['lscher_addr'], c_addr = info_dict['scherl_addr'] ) #core.addListeners(self) <- bu aptal neden calismiyo anlamadim core.openflow.addListenerByName("ConnectionUp", self._handle_ConnectionUp) core.openflow.addListenerByName("FlowStatsReceived", self._handle_FlowStatsReceived) core.openflow.addListenerByName("PacketIn", self._handle_PacketIn )
def __init__(self, nodename, tl_ip, tl_port, dtsl_ip, dtsl_port): self.nodename = nodename self.tl_ip = tl_ip self.tl_port = tl_port self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port # self.s_info_dict = {} #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag='t-dts', proto='udp', _recv_callback=self._handle_recvfromdts, s_addr=(self.tl_ip, self.tl_port), c_addr=(self.dtsl_ip, self.dtsl_port)) # self.session_soft_state_span = 1000 s_soft_expire_timer = threading.Timer( self.session_soft_state_span, self._handle_SessionSoftTimerExpire) s_soft_expire_timer.daemon = True s_soft_expire_timer.start() # logging.info('%s is ready...', self.nodename)
def __init__(self, cl_ip, cl_port_list, dtsl_ip, dtsl_port, dtst_port, proto, rx_type): self.cl_ip = cl_ip self.cl_port_list = cl_port_list self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port self.dtst_port = dtst_port self.proto = proto self.rx_type = rx_type #for control state '''0:start, 1:joined to dts, 2:ready to recv s_data''' self.state = 0 #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'c-dts', proto = 'udp', _recv_callback = self._handle_recvfromdts, s_addr = (self.cl_ip,self.dtst_port), c_addr = (self.dtsl_ip,self.dtsl_port) )
def __init__ (self): # For control comm with scher, ... self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'acter-scher', proto = 'tcp', _recv_callback = self._handle_recvfromscher, s_addr = info_dict['lscher_addr'], c_addr = info_dict['scherl_addr'] ) self.dtsitr_intf = DTSItrCommIntf() # self.dpid_conn_dict = {} core.openflow.addListenerByName("ConnectionUp", self._handle_ConnectionUp) core.openflow.addListenerByName("FlowStatsReceived", self._handle_FlowStatsReceived) core.openflow.addListenerByName("PacketIn", self._handle_PacketIn ) # T be able close dtsitr threads # self.worker_threads = [] # To handle ctrl-c for doing cleanup signal.signal(signal.SIGINT, self.signal_handler)
def __init__(self, nodename, tl_ip, tl_port, dtsl_ip, dtsl_port): self.nodename = nodename self.tl_ip = tl_ip self.tl_port = tl_port self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port # self.s_info_dict = {} #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 't-dts', proto = 'udp', _recv_callback = self._handle_recvfromdts, s_addr = (self.tl_ip,self.tl_port), c_addr = (self.dtsl_ip,self.dtsl_port) ) # self.session_soft_state_span = 1000 s_soft_expire_timer = threading.Timer(self.session_soft_state_span, self._handle_SessionSoftTimerExpire) s_soft_expire_timer.daemon = True s_soft_expire_timer.start() # logging.info('%s is ready...', self.nodename)
class Scheduler(object): #event_chief = EventChief() def __init__(self, xml_network_number): self.gm = GraphMan() if is_scheduler_run: self.xml_parser = XMLParser("net_xmls/net_2p_stwithsingleitr.xml", str(xml_network_number)) else: self.xml_parser = XMLParser( "ext/net_xmls/net_2p_stwithsingleitr.xml", str(xml_network_number)) self.init_network_from_xml() #Useful state variables self.last_sch_req_id_given = -1 self.last_tp_dst_given = info_dict['base_sport'] - 1 #Scher state dicts self.N = 0 #num_activesessions self.sessions_beingserved_dict = {} self.sessions_pre_served_dict = {} self.sid_res_dict = {} self.actual_res_dict = self.gm.give_actual_resource_dict() #for perf plotting self.perf_plotter = PerfPlotter(self.actual_res_dict) #for control_comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag='scher-acter', proto='tcp', _recv_callback=self._handle_recvfromacter, s_addr=info_dict['lacter_addr'], c_addr=info_dict['acterl_addr']) def _handle_recvfromacter(self, msg): #msg = [type_, data_] print 'recvedfromacter msg=%s' % msg def print_scher_state(self): print '<-------------------H--------------------->' print 'is_scheduler_run: ', is_scheduler_run print 'sessions_beingserved_dict:' pprint.pprint(self.sessions_beingserved_dict) print 'sessions_pre_served_dict:' pprint.pprint(self.sessions_pre_served_dict) print '<-------------------E--------------------->' def next_sch_req_id(self): self.last_sch_req_id_given += 1 return self.last_sch_req_id_given def next_tp_dst(self): self.last_tp_dst_given += 1 return self.last_tp_dst_given def welcome_session(self, p_c_ip_list, p_c_gw_list, req_dict, app_pref_dict): """ sch_req_id: should be unique for every sch_session """ #update global var, list and dicts self.N += 1 s_tp_dst_list = [ self.next_tp_dst() for i in range(0, req_dict['parism_level']) ] sch_req_id = self.next_sch_req_id() self.sessions_beingserved_dict.update({ sch_req_id: { 'tp_dst_list': s_tp_dst_list, 'req_dict': req_dict, 'p_c_ip_list': p_c_ip_list, 'p_c_gw_list': p_c_gw_list, 'app_pref_dict': app_pref_dict, 'sch_job_done': False } }) #print 'self.sessions_beingserved_dict: ' #pprint.pprint(self.sessions_beingserved_dict) def bye_session(self, sch_req_id): self.N -= 1 # Send sessions whose "sching job" is done is sent to pre_served category self.sessions_pre_served_dict.update( {sch_req_id: self.sessions_beingserved_dict[sch_req_id]}) del self.sessions_beingserved_dict[sch_req_id] def update_sid_res_dict(self): """ Network resources will be only the ones on the session_shortest path. It resources need to lie on the session_shortest path. """ print '------ update_sid_res_dict ------' #TODO: sessions whose resources are already specified no need for putting them in the loop for s_id in self.sessions_beingserved_dict: p_c_gw_list = self.sessions_beingserved_dict[s_id]['p_c_gw_list'] s_all_paths = self.gm.give_all_paths(p_c_gw_list[0], p_c_gw_list[1]) #print out all_paths for debugging dict_ = {i: p for i, p in enumerate(s_all_paths)} print 's_id:%i, all_paths:' % s_id pprint.pprint(dict_) # for i, p in dict_.items(): p_net_edge_list = self.gm.pathlist_to_netedgelist(p) p_itres_list = self.gm.give_itreslist_on_path(p) if not (s_id in self.sid_res_dict): self.sid_res_dict[s_id] = {'s_info': {}, 'ps_info': {}} self.sid_res_dict[s_id]['ps_info'].update({ i: { 'path': p, 'net_edge_list': p_net_edge_list, 'itres_list': p_itres_list } }) print '---------------- OOO ----------------' def do_sching(self): ''' For currently active sessions, get things together to work sching logic and then send corresponding rules to correspoding actuator (which is a single actuator right now !) ''' alloc_dict = self.allocate_resources() print '---------------SCHING Started ---------------' print 'alloc_dict:' pprint.pprint(alloc_dict) #''' self.perf_plotter.save_sching_result(alloc_dict['general'], alloc_dict['s-wise'], alloc_dict['res-wise']) #''' #Convert sching decs to rules print '**** self.N: ', self.N for s_id in range(0, self.N): s_allocinfo_dict = alloc_dict['s-wise'][s_id] # itwalkinfo_dict = s_allocinfo_dict['itwalkinfo_dict'] p_walk_dict = s_allocinfo_dict['pwalk_dict'] for p_id in range(0, s_allocinfo_dict['parism_level']): p_walk = p_walk_dict[p_id] #Dispatching rule to actuator_actuator sp_walk__tprrule = \ self.get_spwalkrule__sptprrule(s_id, p_id, p_walk = p_walk, pitwalkbundle_dict = itwalkinfo_dict[p_id]) print 'for s_id:%i, p_id:%i;' % (s_id, p_id) #print 'walkrule:' #pprint.pprint(sp_walk__tprrule['walk_rule']) #print 'itjob_rule:' #pprint.pprint(sp_walk__tprrule['itjob_rule']) msg = json.dumps({ 'type': 'sp_sching_dec', 'data': { 's_id': s_id, 'p_id': p_id, 'walk_rule': sp_walk__tprrule['walk_rule'], 'itjob_rule': sp_walk__tprrule['itjob_rule'] } }) self.cci.send_to_client('scher-acter', msg) print '---------------SCHING End---------------' def get_spwalkrule__sptprrule(self, s_id, p_id, p_walk, pitwalkbundle_dict): #print '---> for s_id:%i' % s_id #print 'pitwalkbundle_dict:' #pprint.pprint(pitwalkbundle_dict) #print 'p_walk: ', p_walk s_info_dict = self.sessions_beingserved_dict[s_id] s_tp_dst = s_info_dict['tp_dst_list'][p_id] p_c_ip_list = s_info_dict['p_c_ip_list'] # itjob_rule_dict = {} # walk_rule = [] cur_from_ip = p_c_ip_list[0] cur_to_ip = p_c_ip_list[1] duration = 50 cur_node_str = None for i, node_str in list(enumerate(p_walk)): #node = next_hop if i == 0: cur_node_str = node_str #for adding reverse-walk rule for gw_sw #TODO: 's11-eth1' must be gotten autonomously node = self.gm.get_node(node_str) walk_rule.append({ 'conn': [node['dpid'], cur_to_ip], 'typ': 'forward', 'wc': [cur_to_ip, p_c_ip_list[0], int(s_tp_dst)], 'rule': ['s11-eth1', duration] }) # continue cur_node = self.gm.get_node(cur_node_str) if cur_node['type'] == 't': cur_node_str = node_str continue # node = self.gm.get_node(node_str) edge = self.gm.get_edge(cur_node_str, node_str) if node['type'] == 't': #sw-t walk_rule.append({ 'conn': [cur_node['dpid'], cur_from_ip], 'typ': 'modify_forward', 'wc': [cur_from_ip, cur_to_ip, int(s_tp_dst)], 'rule': [node['ip'], node['mac'], edge['pre_dev'], duration] }) if not (cur_node['dpid'] in itjob_rule_dict): itjob_rule_dict[cur_node['dpid']] = [{ 'tpr_ip': node['ip'], 'tpr_mac': node['mac'], 'swdev_to_tpr': edge['pre_dev'], 'assigned_job': pitwalkbundle_dict['itbundle'][node_str], 'session_tp': int(s_tp_dst), 'consumer_ip': cur_to_ip, 'datasize': pitwalkbundle_dict['p_info']['datasize'] }] else: itjob_rule_dict[cur_node['dpid']].append([{ 'tpr_ip': node['ip'], 'tpr_mac': node['mac'], 'swdev_to_tpr': edge['pre_dev'], 'assigned_job': pitwalkbundle_dict['itbundle'][node_str], 'session_tp': int(s_tp_dst), 'consumer_ip': cur_to_ip, 'datasize': pitwalkbundle_dict['p_info']['datasize'] }]) cur_from_ip = node['ip'] elif node['type'] == 'sw': #sw-sw walk_rule.append({ 'conn': [cur_node['dpid'], cur_from_ip], 'typ': 'forward', 'wc': [cur_from_ip, cur_to_ip, int(s_tp_dst)], 'rule': [edge['pre_dev'], duration] }) cur_from_ip #for reverse walk: data from c to p walk_rule.append({ 'conn': [node['dpid'], cur_to_ip], 'typ': 'forward', 'wc': [cur_to_ip, p_c_ip_list[0], int(s_tp_dst)], 'rule': [edge['post_dev'], duration] }) ''' #to deliver sch_response to src walk_rule.append({'conn':[node['dpid'],info_dict['scher_vip']], 'typ':'forward', 'wc':[info_dict['scher_vip'],p_c_ip_list[0], info_dict['sching_tp_dst']], 'rule':[edge['post_dev'], duration] }) ''' else: raise KeyError('Unknown node_type') cur_node_str = node_str #default rule to forward packet to consumer #TODO: 's12-eth2' must be gotten autonomously walk_rule.append({ 'conn': [12, cur_from_ip], 'typ': 'forward', 'wc': [cur_from_ip, cur_to_ip, int(s_tp_dst)], 'rule': ['s12-eth2', duration] }) """ #default rule to forward sch_response to producer walk_rule.append({'conn':[11,info_dict['scher_vip']], 'typ':'forward', 'wc':[info_dict['scher_vip'],p_c_ip_list[0]], 'rule':['s11-eth1',duration] }) """ return {'walk_rule': walk_rule, 'itjob_rule': itjob_rule_dict} def allocate_resources(self): #returns alloc_dict self.update_sid_res_dict() sching_opter = SchingOptimizer(self.sessions_beingserved_dict, self.actual_res_dict, self.sid_res_dict) sching_opter.solve() # return sching_opter.get_sching_result() def init_network_from_xml(self): node_edge_lst = self.xml_parser.give_node_and_edge_list_from_xml() #print 'node_lst:' #pprint.pprint(node_edge_lst['node_lst']) #print 'edge_lst:' #pprint.pprint(node_edge_lst['edge_lst']) self.gm.graph_add_nodes(node_edge_lst['node_lst']) self.gm.graph_add_edges(node_edge_lst['edge_lst']) def test(self): num_session = 2 ''' sr1: {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3']} {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0} ''' ''' {'data_size':2, 'slack_metric':60, 'func_list':['f1', 'f2']}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3']}, {'data_size':0.1, 'slack_metric':4, 'func_list':['f1']}, ''' req_dict_list = [ { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 1, 'par_share': [1] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, { 'data_size': 1, 'slack_metric': 24, 'func_list': ['f1', 'f2', 'f3'], 'parism_level': 2, 'par_share': [0.5, 0.5] }, ] """ app_pref_dict_list = [ {'m_p': 1,'m_u': 0.1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, ] """ app_pref_dict_list = [ { 'm_p': 10, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 10, 'm_u': 0.5, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 0.5, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, { 'm_p': 1, 'm_u': 1, 'x_p': 0, 'x_u': 0 }, ] p_c_ip_list_list = [ ['10.0.0.2', '10.0.0.1'], ] p_c_gw_list_list = [ ['s11', 's12'], ] for i in range(0, num_session): self.welcome_session(p_c_ip_list_list[0], p_c_gw_list_list[0], req_dict_list[i], app_pref_dict_list[i]) self.do_sching() """
class Scheduler(object): event_chief = EventChief() def __init__(self, xml_net_num, sching_logto): logging.basicConfig(level=logging.DEBUG) self.sching_logto = sching_logto if is_scheduler_run: self.xml_parser = XMLParser("net_2p_stwithsingleitr.xml", str(xml_net_num)) else: self.xml_parser = XMLParser("ext/net_2p_stwithsingleitr.xml", str(xml_net_num)) self.gm = GraphMan() self.init_network_from_xml() #Useful state variables self.last_sching_id_given = -1 self.last_sch_req_id_given = -1 self.last_tp_dst_given = info_dict['base_sport']-1 #Scher state dicts self.num_dstusers = 0 self.users_beingserved_dict = {} #user_ip:{'gw_dpid':<>,'gw_conn_port':<> ...} # self.N = 0 #num_activesessions self.alloc_dict = None self.sessions_beingserved_dict = {} self.sessions_pre_served_dict = {} self.sid_res_dict = {} self.actual_res_dict = self.gm.give_actual_resource_dict() #for perf plotting self.perf_plotter = PerfPlotter(self.actual_res_dict) #for control_comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'scher-acter', proto = 'tcp', _recv_callback = self._handle_recvfromacter, s_addr = info_dict['lacter_addr'], c_addr = info_dict['acterl_addr'] ) ######################### _handle_*** methods ####################### def _handle_recvfromacter(self, msg): #msg = [type_, data_] [type_, data_] = msg if type_ == 'sp_sching_reply': s_id, p_id = int(data_['s_id']), int(data_['p_id']) reply = data_['reply'] if reply == 'done': #updating global dicts self.sessions_beingserved_dict[s_id]['sching_job_done'][p_id] = True #get s_alloc_info s_alloc_info = self.alloc_dict['s-wise'][s_id] s_pl = s_alloc_info['parism_level'] #get s_info, dtsuserinfo s_info = self.sessions_beingserved_dict[s_id] p_ip = s_info['p_c_ip_list'][0] user_info = self.users_beingserved_dict[p_ip] info_dict = {'ip': p_ip, 'mac': user_info['mac'], 'gw_dpid': user_info['gw_dpid'], 'gw_conn_port': user_info['gw_conn_port'] } msg = json.dumps({'type':'sching_reply', 'data':{'parism_level':s_pl, 'p_bw':s_alloc_info['p_bw'][0:s_pl], 'p_tp_dst':s_info['tp_dst_list'][0:s_pl] } }) #fire send_sching_reply to the user Scheduler.event_chief.raise_event('send_msg_to_user',msg,info_dict) else: logging.error('Unexpected reply=%s', reply) def _handle_recvfromdtsuser(self, msg, dtsuserinfo_dict): msg_ = check_msg('recv', 'dts-user', msg) if msg_ == None: print 'msg is not proto-good' return #msg_ = [type_, data_] [type_, data_] = msg_ # src_ip = dtsuserinfo_dict['src_ip'] src_mac = dtsuserinfo_dict['src_mac'] src_gw_dpid = dtsuserinfo_dict['src_gw_dpid'] src_gw_conn_port = dtsuserinfo_dict['src_gw_conn_port'] info_dict = {'ip': src_ip, 'mac': src_mac, 'gw_dpid': src_gw_dpid, 'gw_conn_port': src_gw_conn_port} if type_ == 'join_req': if self.welcome_user(user_ip = src_ip, user_mac = src_mac, gw_dpid = src_gw_dpid, gw_conn_port = src_gw_conn_port ): msg = json.dumps({'type':'join_reply', 'data':'welcome' }) Scheduler.event_chief.raise_event('send_msg_to_user',msg,info_dict) else: #fire send_neg_reply to the user msg = json.dumps({'type':'join_reply', 'data':'sorry' }) Scheduler.event_chief.raise_event('send_msg_to_user',msg,info_dict) elif type_ == 'sching_req': if self.welcome_session(p_c_ip_list = [src_ip, data_['c_ip']], req_dict = data_['req_dict'], app_pref_dict = data_['app_pref_dict'] ): #TODO: for now ... self.do_sching() else: #fire send_neg_reply to the user msg = json.dumps({'type':'sching_reply', 'data':'sorry' }) Scheduler.event_chief.raise_event('send_msg_to_user',msg,info_dict) #################### scher_state_management methods ####################### def print_scher_state(self): print '<---------------------------------------->' print 'is_scheduler_run: ', is_scheduler_run print 'users_beingserved_dict:' pprint.pprint(self.users_beingserved_dict) print 'sessions_beingserved_dict:' pprint.pprint(self.sessions_beingserved_dict) print 'sessions_pre_served_dict:' pprint.pprint(self.sessions_pre_served_dict) print '<---------------------------------------->' def next_sching_id(self): self.last_sching_id_given += 1 return self.last_sching_id_given def next_sch_req_id(self): self.last_sch_req_id_given += 1 return self.last_sch_req_id_given def next_tp_dst(self): self.last_tp_dst_given += 1 return self.last_tp_dst_given def did_user_joindts(self, user_ip): return user_ip in self.users_beingserved_dict def welcome_user(self, user_ip, user_mac, gw_dpid, gw_conn_port): if self.did_user_joindts(user_ip): print 'user_ip=%s already joined' % user_ip return False # self.users_beingserved_dict.update({user_ip:{'gw_dpid':gw_dpid, 'gw_conn_port':gw_conn_port, 'mac': user_mac } }) print 'welcome user: ip=%s, mac=%s, gw_dpid=%s, gw_conn_port=%s' % (user_ip,user_mac,gw_dpid,gw_conn_port) return True #not used now, for future def bye_user(self, user_ip): if not self.did_user_joindts(user_ip): print 'user_ip=%s is not joined' % user_ip return False # del self.users_beingserved_dict[user_ip] print 'bye user: ip=%s' % user_ip return True def welcome_session(self, p_c_ip_list, req_dict, app_pref_dict): ''' sch_req_id: should be unique for every sch_session ''' [p_ip, c_ip] = p_c_ip_list if not (self.did_user_joindts(p_ip) and self.did_user_joindts(c_ip)): print 'nonjoined user in sching_req' return False # p_c_gwtag_list = ['s'+str(self.users_beingserved_dict[p_ip]['gw_dpid']), 's'+str(self.users_beingserved_dict[c_ip]['gw_dpid']) ] #update global var, list and dicts self.N += 1 s_pl = req_dict['parism_level'] s_tp_dst_list = [self.next_tp_dst() for i in range(0,s_pl)] sch_req_id = self.next_sch_req_id() self.sessions_beingserved_dict.update( {sch_req_id:{'tp_dst_list': s_tp_dst_list, 'p_c_ip_list': p_c_ip_list, 'p_c_gwtag_list': p_c_gwtag_list, 'app_pref_dict': app_pref_dict, 'req_dict': req_dict, 'sching_job_done':[False]*s_pl } } ) #print 'self.sessions_beingserved_dict: ' #pprint.pprint(self.sessions_beingserved_dict) # return True def bye_session(self, sch_req_id): self.N -= 1 # Send sessions whose "sching job" is done is sent to pre_served category self.sessions_pre_served_dict.update( {sch_req_id: self.sessions_beingserved_dict[sch_req_id]}) del self.sessions_beingserved_dict[sch_req_id] def init_network_from_xml(self): node_edge_lst = self.xml_parser.give_node_and_edge_list_from_xml() #print 'node_lst:' #pprint.pprint(node_edge_lst['node_lst']) #print 'edge_lst:' #pprint.pprint(node_edge_lst['edge_lst']) self.gm.graph_add_nodes(node_edge_lst['node_lst']) self.gm.graph_add_edges(node_edge_lst['edge_lst']) ######################### sching_rel methods ############################### def update_sid_res_dict(self): """ Network resources will be only the ones on the session_shortest path. It resources need to lie on the session_shortest path. """ logging.info('update_sid_res_dict:') #TODO: sessions whose resources are already specified no need for putting them in the loop for s_id in self.sessions_beingserved_dict: p_c_gwdpid_list = self.sessions_beingserved_dict[s_id]['p_c_gwtag_list'] s_all_paths = self.gm.give_all_paths(p_c_gwdpid_list[0], p_c_gwdpid_list[1]) #print out all_paths for debugging dict_ = {i:p for i,p in enumerate(s_all_paths)} logging.info('s_id=%s, all_paths=\n%s', s_id, pprint.pformat(dict_)) # for i,p in dict_.items(): p_net_edge_list = self.gm.pathlist_to_netedgelist(p) p_itres_list = self.gm.give_itreslist_on_path(p) if not (s_id in self.sid_res_dict): self.sid_res_dict[s_id] = {'s_info':{}, 'ps_info':{}} self.sid_res_dict[s_id]['ps_info'].update( {i: {'path': p, 'net_edge_list': p_net_edge_list, 'itres_list': p_itres_list } }) def do_sching(self): ''' Currently for active sessions, gets things together to work sching logic and then sends corresponding walk/itjob rules to correspoding actuator - which is a single actuator right now ! ''' sching_id = self.next_sching_id() if self.sching_logto == 'file': fname = 'ext/sching_decs/sching_'+sching_id+'.log' logging.basicConfig(filename=fname,filemode='w',level=logging.DEBUG) elif self.sching_logto == 'console': logging.basicConfig(level=logging.DEBUG) else: logging.error('Unexpected sching_logto=%s', self.sching_logto) return # logging.info('<<< sching_id=%s starts >>>', sching_id) self.update_sid_res_dict() sching_opter = SchingOptimizer(self.sessions_beingserved_dict, self.actual_res_dict, self.sid_res_dict ) sching_opter.solve() self.alloc_dict = sching_opter.get_sching_result() logging.info('alloc_dict=\n%s', pprint.pformat(self.alloc_dict)) # """ logging.info('saving sching_dec to figs...') self.perf_plotter.save_sching_result(g_info_dict = self.alloc_dict['general'], s_info_dict = self.alloc_dict['s-wise'], res_info_dict = self.alloc_dict['res-wise']) """ #Convert sching decs to rules for s_id in range(0,self.N): s_allocinfo_dict = self.alloc_dict['s-wise'][s_id] # itwalkinfo_dict = s_allocinfo_dict['itwalkinfo_dict'] p_walk_dict = s_allocinfo_dict['pwalk_dict'] for p_id in range(0,s_allocinfo_dict['parism_level']): p_walk = p_walk_dict[p_id] sp_walk__tprrule = \ self.get_spwalkrule__sptprrule(s_id, p_id, p_walk = p_walk, pitwalkbundle_dict = itwalkinfo_dict[p_id]) logging.info('for s_id=%s, p_id=%s;', s_id, p_id) #print 'walkrule:' #pprint.pprint(sp_walk__tprrule['walk_rule']) #print 'itjob_rule:' #pprint.pprint(sp_walk__tprrule['itjob_rule']) # #Dispatching rule to actuator_actuator msg = json.dumps({'type':'sp_sching_dec', 'data':{'s_id':s_id, 'p_id':p_id, 'walk_rule':sp_walk__tprrule['walk_rule'], 'itjob_rule':sp_walk__tprrule['itjob_rule']} }) self.cci.send_to_client('scher-acter', msg) logging.info('<<< sching_id=%s ends >>>', sching_id) def get_spwalkrule__sptprrule(self,s_id,p_id,p_walk,pitwalkbundle_dict): def get_swportname(dpid, port): return 's'+str(dpid)+'-eth'+str(port) #print '---> for s_id:%i' % s_id #print 'pitwalkbundle_dict:' #pprint.pprint(pitwalkbundle_dict) #print 'p_walk: ', p_walk s_info_dict = self.sessions_beingserved_dict[s_id] s_tp_dst = s_info_dict['tp_dst_list'][p_id] p_c_ip_list = s_info_dict['p_c_ip_list'] # itjob_rule_dict = {} # walk_rule = [] cur_from_ip = p_c_ip_list[0] cur_to_ip = p_c_ip_list[1] duration = 50 cur_node_str = None for i,node_str in list(enumerate(p_walk)):#node = next_hop if i == 0: cur_node_str = node_str #for adding reverse-walk rule for p_gw_sw user_info_dict = self.users_beingserved_dict[cur_from_ip] swportname = get_swportname(dpid = user_info_dict['gw_dpid'], port = user_info_dict['gw_conn_port']) node = self.gm.get_node(node_str) walk_rule.append({'conn':[node['dpid'],cur_to_ip], 'typ':'forward', 'wc':[cur_to_ip,p_c_ip_list[0],int(s_tp_dst)], 'rule':[swportname, duration] }) # continue cur_node = self.gm.get_node(cur_node_str) if cur_node['type'] == 't': cur_node_str = node_str continue # node = self.gm.get_node(node_str) edge = self.gm.get_edge(cur_node_str, node_str) if node['type'] == 't': #sw-t walk_rule.append({'conn':[cur_node['dpid'],cur_from_ip], 'typ':'modify_forward', 'wc':[cur_from_ip,cur_to_ip,int(s_tp_dst)], 'rule':[node['ip'],node['mac'],edge['pre_dev'],duration] }) if not (cur_node['dpid'] in itjob_rule_dict): itjob_rule_dict[cur_node['dpid']] = [{ 'tpr_ip':node['ip'], 'tpr_mac':node['mac'], 'swdev_to_tpr':edge['pre_dev'], 'assigned_job': pitwalkbundle_dict['itbundle'][node_str], 'session_tp': int(s_tp_dst), 'consumer_ip': cur_to_ip, 'datasize': pitwalkbundle_dict['p_info']['datasize'] }] else: itjob_rule_dict[cur_node['dpid']].append( [{ 'tpr_ip':node['ip'], 'tpr_mac':node['mac'], 'swdev_to_tpr':edge['pre_dev'], 'assigned_job':pitwalkbundle_dict['itbundle'][node_str], 'session_tp': int(s_tp_dst), 'consumer_ip': cur_to_ip, 'datasize': pitwalkbundle_dict['p_info']['datasize'] }] ) cur_from_ip = node['ip'] elif node['type'] == 'sw': #sw-sw walk_rule.append({'conn':[cur_node['dpid'],cur_from_ip], 'typ':'forward', 'wc':[cur_from_ip,cur_to_ip,int(s_tp_dst)], 'rule':[edge['pre_dev'], duration] }) cur_from_ip #for reverse walk: data from c to p walk_rule.append({'conn':[node['dpid'],cur_to_ip], 'typ':'forward', 'wc':[cur_to_ip,p_c_ip_list[0],int(s_tp_dst)], 'rule':[edge['post_dev'], duration] }) ''' #to deliver sch_response to src walk_rule.append({'conn':[node['dpid'],info_dict['scher_vip']], 'typ':'forward', 'wc':[info_dict['scher_vip'],p_c_ip_list[0], info_dict['sching_port']], 'rule':[edge['post_dev'], duration] }) ''' else: raise KeyError('Unknown node_type') cur_node_str = node_str #default rule to forward packet to consumer user_info_dict = self.users_beingserved_dict[cur_to_ip] swportname = get_swportname(dpid = user_info_dict['gw_dpid'], port = user_info_dict['gw_conn_port']) walk_rule.append({'conn':[user_info_dict['gw_dpid'],cur_from_ip], 'typ':'forward', 'wc':[cur_from_ip,cur_to_ip,int(s_tp_dst)], 'rule':[swportname,duration] }) """ #default rule to forward sch_response to producer walk_rule.append({'conn':[11,info_dict['scher_vip']], 'typ':'forward', 'wc':[info_dict['scher_vip'],p_c_ip_list[0]], 'rule':['s11-eth1',duration] }) """ return {'walk_rule':walk_rule, 'itjob_rule':itjob_rule_dict} ############################################################################## def test(self): userinfo_list = [ {'user_ip':'10.0.0.2','user_mac':'00:00:00:01:00:02','gw_dpid':11,'gw_conn_port':1}, {'user_ip':'10.0.0.1','user_mac':'00:00:00:01:00:01','gw_dpid':12,'gw_conn_port':2} ] for userinfo in userinfo_list: self.welcome_user(user_ip = userinfo['user_ip'], user_mac = userinfo['user_mac'], gw_dpid = userinfo['gw_dpid'], gw_conn_port = userinfo['gw_conn_port'] ) # num_session = 1 req_dict_list = [ {'data_size':4, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':1, 'par_share':[1]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, {'data_size':1, 'slack_metric':24, 'func_list':['f1','f2','f3'], 'parism_level':2, 'par_share':[0.5, 0.5]}, ] app_pref_dict_list = [ {'m_p': 10,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 10,'m_u': 0.5,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 0.5,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, {'m_p': 1,'m_u': 1,'x_p': 0,'x_u': 0}, ] p_c_ip_list_list = [ ['10.0.0.2','10.0.0.1'], ] for i in range(0, num_session): self.welcome_session(p_c_ip_list = p_c_ip_list_list[0], req_dict = req_dict_list[i], app_pref_dict = app_pref_dict_list[i] ) self.do_sching()
class Consumer(object): def __init__(self, cl_ip, cl_port_list, dtsl_ip, dtsl_port, dtst_port, proto, rx_type): self.cl_ip = cl_ip self.cl_port_list = cl_port_list self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port self.dtst_port = dtst_port self.proto = proto self.rx_type = rx_type #for control state '''0:start, 1:joined to dts, 2:ready to recv s_data''' self.state = 0 #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'c-dts', proto = 'udp', _recv_callback = self._handle_recvfromdts, s_addr = (self.cl_ip,self.dtst_port), c_addr = (self.dtsl_ip,self.dtsl_port) ) def _handle_recvfromdts(self, msg): #msg = [type_, data_] [type_, data_] = msg if type_ == 'join_reply': if self.state != 0: logging.error('join_reply: unexpected cur_state=%s', self.state) return # if data_ == 'welcome': self.state = 1 logging.info('joined to dts :) data_=%s', data_) #immediately start s_recving_servers self.start_recvers() elif data_ == 'sorry': logging.info('cannot join to dts :( data_=%s', data_) def start_recvers(self): if self.state != 1: logging.error('stream_sdata: unexpected cur_state=%s', self.state) return # for port in self.cl_port_list: addr = (self.cl_ip, port) recver = Receiver(laddr = addr, proto = self.proto, rx_type = self.rx_type, file_url = 'rx_%s.dat' % port ) logging.info('recver started at addr=%s', addr) # self.state = 2 def send_join_req(self): if self.state != 0: logging.error('send_join_req: unexpected cur_state=%s', self.state) return # msg = json.dumps({'type':'join_req', 'data':''}) self.cci.send_to_client('c-dts',msg) def test(self): self.send_join_req() """
def main(): cci = ControlCommIntf() cci.reg_commpair('scher-acter','udp',recv_data_handler,('127.0.0.1',7000),('127.0.0.1',6000)) # raw_input('Enter')
class Producer(object): def __init__(self, intf, pl_port, dtsl_ip, dtsl_port, cl_ip, proto, tx_type, file_url, req_dict,app_pref_dict, htbdir): self.intf = intf self.pl_ip = get_addr(intf) self.pl_port = pl_port self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port self.cl_ip = cl_ip self.proto = proto self.tx_type = tx_type self.file_url = file_url self.req_dict = req_dict self.app_pref_dict = app_pref_dict self.htbdir = htbdir #for control state '''0:start, 1:joined to dts, 2:sch_reply is recved''' self.state = 0 #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'p-dts', proto = 'udp', _recv_callback = self._handle_recvfromdts, s_addr = (self.pl_ip,self.pl_port), c_addr = (self.dtsl_ip,self.dtsl_port) ) #for htb_queue conf - bw shaping self.ktif = 230 #kernel timer interrupt frequency (Hz) self.init_htbdir() ######################## _handle_*** ######################## def _handle_recvfromdts(self, msg): #msg = [type_, data_] [type_, data_] = msg if type_ == 'sching_reply': if self.state != 1: logging.error('sching_reply: unexpected cur_state=%s', self.state) return # if data_ != 'sorry': self.state = 2 logging.info('successful sch_req :) data_=') logging.info('%s', pprint.pformat(data_)) #immediately start streaming session data pl = int(data_['parism_level']) p_bw = data_['p_bw'] p_tp_dst = data_['p_tp_dst'] # self.init_htbconf(pl, p_bw, p_tp_dst) datasize = float(self.req_dict['data_size']) for i in range(0,pl): self.stream_sdata(datasize = datasize*float(self.req_dict['par_share'][i]), cl_port = p_tp_dst[i] ) else: logging.info('unsuccessful sch_req :( data_=%s', data_) return elif type_ == 'join_reply': if self.state != 0: logging.error('join_reply: unexpected cur_state=%s', self.state) return # if data_ == 'welcome': self.state = 1 logging.info('joined to dts :) data_=%s', data_) #send immediately sching_req self.send_sching_req() elif data_ == 'sorry': logging.info('cannot join to dts :( data_=%s', data_) ######################## htb conf ######################## def init_htbdir(self): dir_ = '%s/%s' % (self.htbdir, self.intf) # if not os.path.exists(dir_): os.makedirs(dir_) logging.debug('dir=%s is made', dir_) else: #dir already exists, clean it self.clean_dir(dir_) # #for htb.init.sh - need to put filename=self.intf EVEN IF IT IS EMPTY. #(opt: DEFAULT=0 to make unclassified traffic performance as high as possible) self.write_to_file(self.intf,'DEFAULT=0') def clean_dir(self, dir_): for f in os.listdir(dir_): f_path = os.path.join(dir_, f) try: if os.path.isfile(f_path): os.unlink(f_path) logging.debug('file=%s is deleted', f) except Exception, e: logging.error('%s', e)
class Consumer(object): def __init__(self, cl_ip, cl_port_list, dtsl_ip, dtsl_port, dtst_port, proto, rx_type, logto): self.cl_ip = cl_ip self.cl_port_list = cl_port_list self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port self.dtst_port = dtst_port self.proto = proto self.rx_type = rx_type self.logto = logto #for control state '''0:start, 1:joined to dts, 2:ready to recv s_data''' self.state = 0 #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag = 'c-dts', proto = 'udp', _recv_callback = self._handle_recvfromdts, s_addr = (self.cl_ip,self.dtst_port), c_addr = (self.dtsl_ip,self.dtsl_port) ) def _handle_recvfromdts(self, msg): #msg = [type_, data_] [type_, data_] = msg if type_ == 'join_reply': if self.state != 0: logging.error('join_reply: unexpected cur_state=%s', self.state) return # if data_ == 'welcome': self.state = 1 logging.info('joined to dts :) data_=%s', data_) #immediately start s_recving_servers self.start_recvers() elif data_ == 'sorry': logging.info('cannot join to dts :( data_=%s', data_) def start_recvers(self): if self.state != 1: logging.error('stream_sdata: unexpected cur_state=%s', self.state) return # for port in self.cl_port_list: addr = (self.cl_ip, port) recver = Receiver(laddr = addr, proto = self.proto, rx_type = self.rx_type, file_url = 'rx_%s.dat' % port, logto = self.logto ) logging.info('recver started at addr=%s', addr) # self.state = 2 def send_join_req(self): if self.state != 0: logging.error('send_join_req: unexpected cur_state=%s', self.state) return # msg = json.dumps({'type':'join_req', 'data':''}) self.cci.send_to_client('c-dts',msg) def test(self): self.send_join_req() """
class Actuator(object): def __init__(self): #TODO: active sching_realization #for control comm with scher, ... self.cci = ControlCommIntf() self.cci.reg_commpair(sctag='acter-scher', proto='tcp', _recv_callback=self._handle_recvfromscher, s_addr=info_dict['lscher_addr'], c_addr=info_dict['scherl_addr']) #core.addListeners(self) <- bu aptal neden calismiyo anlamadim core.openflow.addListenerByName("ConnectionUp", self._handle_ConnectionUp) core.openflow.addListenerByName("FlowStatsReceived", self._handle_FlowStatsReceived) core.openflow.addListenerByName("PacketIn", self._handle_PacketIn) ######################### _handle_*** methods ####################### def _handle_recvfromscher(self, msg): #msg = [type_, data_] [type_, data_] = msg if type_ == 'sp_sching_dec': s_id, p_id = int(data_['s_id']), int(data_['p_id']) walk_rule = data_['walk_rule'] itjob_rule = data_['itjob_rule'] #print 'walk_rule: ' #pprint.pprint(walk_rule) #updating global dicts based on the input rxed from scher if not (s_id in info_dict['sid_pidlist_dict']): info_dict['sid_pidlist_dict'][s_id] = [] info_dict['sid_pidlist_dict'][s_id].append(p_id) # ruleparser.modify_schedwalkxmlfile_by_walkrule( str(s_id), str(p_id), walk_rule) ruleparser.modify_scheditjobxmlfile_by_itjobrule( str(s_id), str(p_id), itjob_rule) if _install_schrules_proactively: self.install_proactive_schedwalk(s_id, p_id) self.install_proactive_scheditjob(s_id, p_id) # Send "I am done with the job(sch realization)" TO SCHER_CONT msg = json.dumps({'type': 'sp_sching_reply', 'data': 'done'}) self.cci.send_to_client('acter-scher', msg) elif type_ == 'sth_else': pass #Since the SW rules are set proactively from the beginning no packet_in is expected ! def _handle_PacketIn(self, event): packet = event.parsed print '---> handle_data_packet is called;' ip = packet.find('ipv4') if ip is None: print "packet", packet, " isn't IP!" return print "Rxed packet: ", packet, "from sw_dpid: ", dpidToStr( event.connection.dpid) print "Src IP:%s, Dst IP: %s" % (ip.srcip, ip.dstip) def _handle_ConnectionUp(self, event): print "Connection %s" % (event.connection) if _install_deneme_flow and event.connection.dpid == 3: print "Sending deneme_flow to sw_dpid:%s " % ( event.connection.dpid) self.send_ofmod_forward('handle_conn_up', event.connection, '10.0.0.32', '10.0.0.31', 6000, 4, info_dict['s_entry_dur']) def _handle_FlowStatsReceived(self, event): stats = flow_stats_to_list(event.stats) print "FlowStatsReceived from ", dpidToStr( event.connection.dpid), ": ", stats #ofcourse works only for mininet networks def dev_tfport(self, dev_str): eth_part = dev_str.split('-', 1)[1] return int(eth_part.strip('eth')) ######################### install_*** methods ####################### def install_proactive_scheditjob(self, s_id, p_id): dict_ = ruleparser.get_itjobruledict_forsp(str(s_id), str(p_id)) print 'itjobdict:' pprint.pprint(dict_) for conn in core.openflow.connections: dpid = str(conn.dpid) try: itnodeinfo_list = dict_[dpid] except KeyError: #sw is not connected to any itnode on the sched walk continue for itnodeinfo in itnodeinfo_list: jobinfo = itnodeinfo['jobinfo'] walkinfo = itnodeinfo['walkinfo'] # self.send_udp_packet_out(conn=conn, fw_port=self.dev_tfport( str(walkinfo['swdev_to_node'])), payload=json.dumps({ 'type': 'itjobrule', 'data': jobinfo }), tp_src=info_dict['sching_tp_src'], tp_dst=info_dict['sching_tp_dst'], src_ip=info_dict['acter_vip'], dst_ip=walkinfo['node_ip'], src_mac=info_dict['acter_vmac'], dst_mac=walkinfo['node_mac']) def install_proactive_schedwalk(self, s_id, p_id): [dict_I, hmfromdpid_dict ] = ruleparser.get_walkruledict_forsp(str(s_id), str(p_id)) #print 'walkruledict:' #pprint.pprint(dict_I) #print 'hmfromdpid_dict:' #pprint.pprint(hmfromdpid_dict) for conn in core.openflow.connections: dpid = str(conn.dpid) #str(event.connection.dpid) try: hm = hmfromdpid_dict[dpid] except (KeyError): print '\n# No entry in hm_from_dpid for dpid=%s' % dpid continue l_dict = None counter = 0 while (counter <= hm): l_dict = dict_I[dpid, counter] typ = l_dict['typ'] rule_dict = l_dict['rule_dict'] wc_dict = l_dict['wc_dict'] if typ == 'forward': self.send_ofmod_forward( 'initial_flows', conn, wc_dict['src_ip'], wc_dict['dst_ip'], wc_dict['tp_dst'], self.dev_tfport(rule_dict['fport']), info_dict['s_entry_dur']) #self.send_stat_req(conn) elif typ == 'modify_forward': self.send_ofmod_modify_forward( 'initial_flows', conn, wc_dict['src_ip'], wc_dict['dst_ip'], wc_dict['tp_dst'], rule_dict['new_dst_ip'], rule_dict['new_dst_mac'], self.dev_tfport(rule_dict['fport']), info_dict['s_entry_dur']) #self.send_stat_req(conn) counter += 1 ####################### send_*** methods ################################### # Method for just sending a UDP packet over any sw_port (broadcast by default) def send_udp_packet_out(self, conn, payload, tp_src, tp_dst, src_ip, dst_ip, src_mac, dst_mac, fw_port=of.OFPP_ALL): msg = of.ofp_packet_out(in_port=of.OFPP_NONE) msg.buffer_id = None #Make the udp packet udpp = pkt.udp() udpp.srcport = tp_src udpp.dstport = tp_dst udpp.payload = payload #Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.UDP_PROTOCOL ipp.srcip = IPAddr(src_ip) ipp.dstip = IPAddr(dst_ip) # Ethernet around that... ethp = pkt.ethernet() ethp.src = EthAddr(src_mac) ethp.dst = EthAddr(dst_mac) ethp.type = ethp.IP_TYPE # Hook them up... ipp.payload = udpp ethp.payload = ipp # Send it to the sw msg.actions.append(of.ofp_action_output(port=fw_port)) msg.data = ethp.pack() #show msg before sending """ print '*******************' print 'msg.show(): ',msg.show() print '*******************' """ print "self.send_udp_packet_out; sw%s and fw_port:%s" % (conn.dpid, fw_port) conn.send(msg) #Basic send functions for communicating with SWs def send_clear_swtable(self, conn): msg = of.ofp_flow_mod(command=of.OFPFC_DELETE) conn.send(msg) print 'clearing flows from %s.' % dpid_to_str(event.connection.dpid) def send_stat_req(self, conn): conn.send(of.ofp_stats_request(body=of.ofp_flow_stats_request())) print "\nsend_stat_req to sw_dpid=%s\n" % conn.dpid def send_ofmod_delete(self, conn, nw_src, nw_dst, tp_dst, duration): msg = of.ofp_flow_mod() msg.command = OFPFC_DELETE #wcs msg.match.dl_type = 0x800 # Ethertype / length (e.g. 0x0800 = IPv4) msg.match.nw_src = IPAddr(nw_src) msg.match.nw_dst = IPAddr(nw_dst) msg.match.nw_proto = 17 #UDP msg.match.tp_dst = int(tp_dst) # msg.idle_timeout = duration[0] msg.hard_timeout = duration[1] conn.send(msg) print '\nsend_ofmod_delete to sw_dpid=%s' % conn.dpid print 'wcs: src_ip=%s, dst_ip=%s, tp_dst=%s\n' % (nw_src, nw_dst, tp_dst) def send_ofmod_forward(self, _called_from, conn, nw_src, nw_dst, tp_dst, fport, duration): msg = of.ofp_flow_mod() #msg.match = of.ofp_match.from_packet(packet) msg.priority = 0x7000 #msg.match = of.ofp_match(dl_type = pkt.ethernet.IP_TYPE, nw_proto = pkt.ipv4.UDP_PROTOCOL, nw_dst=IPAddr(nw_dst)) msg.match.dl_type = 0x800 # Ethertype / length (e.g. 0x0800 = IPv4) msg.match.nw_src = IPAddr(nw_src) msg.match.nw_dst = IPAddr(nw_dst) msg.match.nw_proto = 17 #UDP if tp_dst != None: msg.match.tp_dst = int(tp_dst) msg.idle_timeout = duration[0] msg.hard_timeout = duration[1] #print "event.ofp.buffer_id: ", event.ofp.buffer_id if _called_from == 'packet_in': msg.buffer_id = event.ofp.buffer_id msg.actions.append(of.ofp_action_output(port=fport)) conn.send(msg) print '\nsend_ofmod_forward to sw_dpid=%s' % conn.dpid print 'wcs: src_ip=%s, dst_ip=%s, tp_dst=%s' % (nw_src, nw_dst, tp_dst) print 'acts: fport=%s\n', fport def send_ofmod_modify_forward(self, _called_from, conn, nw_src, nw_dst, tp_dst, new_dst, new_dl_dst, fport, duration): msg = of.ofp_flow_mod() msg.priority = 0x7000 msg.match.dl_type = 0x800 # Ethertype / length (e.g. 0x0800 = IPv4) msg.match.nw_src = IPAddr(nw_src) msg.match.nw_dst = IPAddr(nw_dst) msg.match.nw_proto = 17 #UDP if tp_dst != None: msg.match.tp_dst = int(tp_dst) msg.idle_timeout = duration[0] msg.hard_timeout = duration[1] if _called_from == 'packet_in': msg.buffer_id = event.ofp.buffer_id msg.actions.append( of.ofp_action_nw_addr(nw_addr=IPAddr(new_dst), type=7)) msg.actions.append( of.ofp_action_dl_addr(dl_addr=EthAddr(new_dl_dst), type=5)) msg.actions.append(of.ofp_action_output(port=fport)) conn.send(msg) print '\nsend_ofmod_modify_forward to sw_dpid=%s' % conn.dpid print 'wcs: src_ip=%s, dst_ip=%s, tp_dst=%s' % (nw_src, nw_dst, tp_dst) print 'acts: new_dst=%s, new_dl_dst=%s, fport=%s\n' % ( new_dst, new_dl_dst, fport)
class Transit(object): def __init__(self, nodename, tl_ip, tl_port, dtsl_ip, dtsl_port): self.nodename = nodename self.tl_ip = tl_ip self.tl_port = tl_port self.dtsl_ip = dtsl_ip self.dtsl_port = dtsl_port # self.s_info_dict = {} #for control comm self.cci = ControlCommIntf() self.cci.reg_commpair(sctag='t-dts', proto='udp', _recv_callback=self._handle_recvfromdts, s_addr=(self.tl_ip, self.tl_port), c_addr=(self.dtsl_ip, self.dtsl_port)) # self.session_soft_state_span = 1000 s_soft_expire_timer = threading.Timer( self.session_soft_state_span, self._handle_SessionSoftTimerExpire) s_soft_expire_timer.daemon = True s_soft_expire_timer.start() # logging.info('%s is ready...', self.nodename) def _handle_SessionSoftTimerExpire(self): while True: logging.info('_handle_SessionSoftTimerExpire;') #print 's_info_dict:' #pprint.pprint(s_info_dict ) for s_tp_dst, s_info in self.s_info_dict.items(): inactive_time_span = time.time() - s_info['s_active_last_time'] if inactive_time_span >= self.session_soft_state_span: #soft state expire s_info['s_server'].shutdown() s_info['s_sock'].close() del self.s_info_dict[s_tp_dst] logging.info( 'inactive_time_span=%s\ns with tp_dst:%s is soft-expired.', inactive_time_span, s_tp_dst) # logging.info('------') # do every ... secs time.sleep(self.session_soft_state_span) ########################## handle dts_comm ################################# def _handle_recvfromdts(self, msg): #msg = [type_, data_] [type_, data_] = msg if type_ == 'itjob_rule': self.welcome_s(data_) def welcome_s(self, data_): #If new_s with same tpdst arrives, old_s is overwritten by new_s stpdst = int(data_['s_tp']) if stpdst in self.s_info_dict: self.bye_s(stpdst) #updating global dicts del data_['s_tp'] jobtobedone = {ftag:1000*data_['datasize']*comp/func_comp_dict[ftag] \ for ftag,comp in data_['itfunc_dict'].items()} data_.update({'jobtobedone': jobtobedone}) #calc est_prot est_proct = proc_time_model(datasize=float(data_['datasize']), func_comp=float(data_['comp']), proc_cap=float(data_['proc'])) # self.s_info_dict[stpdst] = { 'itjobrule': data_, 's_server': self.create_s_server(stpdst), 's_sock': self.create_s_sock(), 's_active_last_time': time.time(), 'est_proct': est_proct } # logging.info('welcome new_s; tpdst=%s, s_info=\n%s', stpdst, pprint.pformat(self.s_info_dict[stpdst])) def bye_s(self, stpdst): self.s_info_dict[stpdst]['s_server'].shutdown() self.s_info_dict[stpdst]['s_sock'].close() #ready to erase s_info del self.s_info_dict[stpdst] logging.info('bye s; tpdst=%s', stpdst) ######################### handle s_data_traffic ############################ def create_s_sock(self): #return udp sock return socket.socket(socket.AF_INET, socket.SOCK_DGRAM) def create_s_server(self, port): s_addr = (self.tl_ip, port) s_server = ThreadedUDPServer(self._handle_recvsdata, s_addr, ThreadedUDPRequestHandler) s_server_thread = threading.Thread(target=s_server.serve_forever) s_server_thread.daemon = True s_server_thread.start() # logging.info('udp_server is started at s_addr=%s', s_addr) return s_server def _handle_recvsdata(self, s_tp_dst, data): if not s_tp_dst in self.s_info_dict: raise NoItruleMatchError('No itjobrule match', s_tp_dst) return data = self.proc_pipeline(s_tp_dst=s_tp_dst, data=data) self.forward_data(s_tp_dst, data) def proc_pipeline(self, s_tp_dst, data): global itfunc_dict itjobrule = self.s_info_dict[s_tp_dst]['itjobrule'] jobtobedone = itjobrule['jobtobedone'] proc_cap = itjobrule['proc'] #datasize_ = 8*len(data) #in bits for ftag, compleft in jobtobedone.items(): if jobtobedone[ftag] > 0: datasize = 8 * sys.getsizeof(data) data = itfunc_dict[ftag](datasize, data, proc_cap) #update jobtobedone jobtobedone[ftag] -= datasize # return data def forward_data(self, s_tp_dst, data): s_info = self.s_info_dict[s_tp_dst] # to_ip = s_info['itjobrule']['data_to_ip'] sock = s_info['s_sock'] sock.sendto(data, (to_ip, int(s_tp_dst))) logging.info('proced data is forwarded to_ip:%s', to_ip) #update session_active_last_time s_info['s_active_last_time'] = time.time()