def process_register_values(self, read_qid, register): store = {} if self.tuple_dict.keys(): read_register_cmds = "" for indexLoc in self.tuple_dict.keys(): store[indexLoc] = self.tuple_dict[indexLoc]['tuple'] read_register_cmds += "register_read " + register + " " + str( indexLoc) + "\n" success, out = get_out("echo \"" + read_register_cmds + "\" | " + self.bmv2_cli + " --thrift-port " + str(self.thrift_port) + " | grep -o -e \"$1.*[1-9][0-9]*$\"") output = {} if success: write_register_cmds = "" for line in out.split('\n'): if line: m = re.search('.*\[(.*)\]\=\s+(.*)', out) output[m.group(1)] = m.group(2) write_register_cmds += "register_write " + register + " " + str( m.group(1)) + " 0\n" success3, out3 = get_out("echo \"" + write_register_cmds + "\" | " + self.bmv2_cli + " --thrift-port " + str(self.thrift_port)) for indexLoc in store.keys(): if str(indexLoc) in output.keys(): out = store[indexLoc] + "," + output[str(indexLoc)] self.logger.info(out) self.tuple_dict.pop(indexLoc, None)
def process_register_values(self, read_qid, register): cnx = mysql.connector.connect(**self.db_conf) cursor = cnx.cursor(buffered=True) query = "SELECT id, qid, tuple, indexLoc FROM indexStore where qid = %d"%(read_qid) cursor.execute(query) store = {} with open(self.read_file, 'w') as f: for (id, qid, tuple, indexLoc) in cursor: store[indexLoc] = {'tuple': tuple, 'id': id } f.write("register_read "+register+" " + str(indexLoc) + "\n") f.flush() f.close() cursor.close() cnx.close() success, out = get_out(self.bmv2_cli + " --thrift-port " + str(self.thrift_port) + " < " + self.read_file + " | grep -o -e \"$1.*[1-9][0-9]*$\"") output = {} if success: with open(self.write_file, 'w') as f: for line in out.split('\n'): if line: m = re.search('.*\[(.*)\]\=\s+(.*)', line) output[m.group(1)] = m.group(2) f.write("register_write "+register+" " + str(m.group(1)) + " 0\n") f.flush() f.close() success3, out3 = get_out(self.bmv2_cli + " --thrift-port " + str(self.thrift_port) + " < " + self.write_file) ids = [] for indexLoc in store.keys(): if str(indexLoc) in output.keys(): out = store[indexLoc]['tuple'] + ","+output[str(indexLoc)] self.logger.info(out) self.send_data(out + "\n") ids.append(store[indexLoc]['id']) if ids: ids_str = ",".join([str(id) for id in ids]) delete_indexes = ("DELETE FROM indexStore WHERE id in ("+ids_str+")") cnx = mysql.connector.connect(**self.db_conf) cursor = cnx.cursor(buffered=True) cursor.execute(delete_indexes) cnx.commit() cursor.close() cnx.close()
def initialize(self, p4_json_path, p4_commands_path): self.logger.info('initialize') self.create_interfaces() get_out( "sudo ps -ef | grep simple_switch | grep -v grep | awk '{print $2}' | sudo xargs kill -9" ) sleep(1) cmd = self.switch_path + " >/dev/null 2>&1" get_out(cmd) self.switch = Switch(p4_json_path, self.switch_path, self.internal_interfaces) self.switch.start() print "\nWaiting for switch to start..." sleep(2) print "Sending commands..." self.send_commands(p4_json_path, p4_commands_path) sleep(1)
} p4_src, p4_commands, registers = get_sequential_code(NUMBER_OF_REGISTERS) write_to_file(P4_COMPILED, p4_src) commands_string = "\n".join(p4_commands) write_to_file(P4_COMMANDS, commands_string) dataplane = P4DataPlane(interfaces, SWITCH_PATH, CLI_PATH, THRIFTPORT, P4C_BM_SCRIPT) dataplane.compile_p4(P4_COMPILED, JSON_P4_COMPILED) dataplane.create_interfaces() cmd = dataplane.switch_path + " >/dev/null 2>&1" get_out(cmd) # initialize_the_switch(JSON_P4_COMPILED, SWITCH_PATH) Switch(JSON_P4_COMPILED, SWITCH_PATH).start() time.sleep(1) dataplane.send_commands(JSON_P4_COMPILED, P4_COMMANDS) reset_commands = ["register_reset %s" % (i) for i in registers] reset_commands_string = "\n".join(reset_commands) write_to_file(P4_DELTA_COMMANDS, reset_commands_string) logger = create_return_logger(BASE_PATH + "results/registers.log") start = time.time() dataplane.send_commands(JSON_P4_COMPILED, P4_DELTA_COMMANDS) end = time.time()
def disable_ipv6(self, interface): base = "sysctl net.ipv6.conf.%s.disable_ipv6=1" % (interface) (returncode, error) = get_out(base) return returncode
def put_toe_option_off(self, interface, toe_option): base = "/sbin/ethtool --offload %s \"%s\" off" % (interface, toe_option) (returncode, error) = get_out(base) return returncode
def put_link_up(self, interface): base = "ip link set dev %s up" % (interface) (returncode, error) = get_out(base) return returncode
def set_peer(self): base = "ip link add name %s type veth peer name %s" % (self.inName, self.outName) # print base (returncode, error) = get_out(base) return returncode
def check_link(self): cmd = "ip link show %s &> /dev/null" % (self.inName) (returncode, error) = get_out(cmd) return returncode
def reset_switch_state(self): self.logger.info('reset switch state') cmd = "echo \'reset_state\' | " + self.cli_path + " --thrift-port " + str( self.thrift_port) get_out(cmd)
def compile_p4(self, p4_compiled, json_p4_compiled): self.logger.info('compile p4 to json') CMD = self.bm_script + " " + p4_compiled + " --json " + json_p4_compiled get_out(CMD)