def interface_set_aggregate_id(oc_yph, pkey_ar, val, is_create, disp_args): # not support to create port interface if is_create: return False is_remove = True if val == "" else False if is_remove: # get old pc name pc_name = interface_get_old_pc_name_by_port(pkey_ar[0], disp_args) if not pc_name: return True else: pc_name = val # set port down before adding port to port channel exec_cmd = 'ifconfig %s down' % pkey_ar[0] util_utl.utl_execute_cmd(exec_cmd) # use teamdctl to add/remove port exec_cmd = TEAMD_CFG_PORT_CMD_TMPL.format(pc_name, ["add", "remove"][is_remove], pkey_ar[0]) ret_val = util_utl.utl_execute_cmd(exec_cmd) if is_remove: interface_restore_port_setting(disp_args.appdb, pkey_ar[0]) return ret_val
def nwi_pf_delete_rule(root_yph, pkey_ar, disp_args): try: rule_name = pkey_ar[1].replace(" ", "_").replace( "-", "_").upper().encode('ascii') table_name = pkey_ar[0].replace(" ", "_").replace( "-", "_").upper().encode('ascii') acl_cfgs = {} if os.path.exists(ACL_JSON_FILE): with open(ACL_JSON_FILE) as infile: acl_cfgs = json.load(infile) if "acl" not in acl_cfgs: return True if table_name not in acl_cfgs["acl"]["acl-sets"]["acl-set"]: return True table_cfgs = acl_cfgs["acl"]["acl-sets"]["acl-set"][table_name] if "acl-entries" not in table_cfgs: return True rules = table_cfgs["acl-entries"]["acl-entry"] if rule_name in rules: del rules[rule_name] if not rules: del acl_cfgs["acl"]["acl-sets"]["acl-set"][table_name] with open(ACL_JSON_FILE, 'w') as outfile: json.dump(acl_cfgs, outfile) util_utl.utl_execute_cmd( "acl-loader update full {}".format(ACL_JSON_FILE)) except: return False return True
def interface_set_ip_v4(oc_yph, pkey_ar, val, is_create, disp_args): try: ip_cfg = {} if val == "" else eval(val) ip_new = ip_cfg["ip"] ip_pfx = ip_cfg["prefix-length"] except: return False is_del = True if ip_new == "0" or ip_new == "" else False ret_val = interface_db_set_ip(disp_args.cfgdb, not is_del, pkey_ar[0], pkey_ar[1] + '/' + str(ip_pfx)) if not IS_NEW_TEAMMGRD: # only ip on vlan interface can take effect immediately if pkey_ar[0].startswith('Vlan'): return ret_val if ret_val: exec_cmd = "ip addr {0} {1}/{2} dev {3}".format(['add', 'del'][is_del], pkey_ar[1], ip_pfx, pkey_ar[0]) util_utl.utl_execute_cmd(exec_cmd) return ret_val
def lr_set_route_v4(oc_yph, pkey_ar, val, is_create, disp_args): try: rt_cfg = {} if val == "" else eval(val) nh_str = "" for k, v in rt_cfg.items(): rt_inf = "" if v.has_key('interface-ref'): rt_inf = v['interface-ref']['config']['interface'] rt_nh = v['config']['next-hop'] if rt_inf != "": nh_tmp = "nexthop vrf Vrf-{0} {1} dev {2}".format(pkey_ar[0], rt_nh, rt_inf) else: nh_tmp = "nexthop vrf Vrf-{0} {1}".format(pkey_ar[0], rt_nh) nh_str = " ".join([nh_str, nh_tmp]) except: return False # {0} : add/del # {1} : 172.17.2.0/24 # {2} : nexthop vrf Vrf-17693 10.0.0.108 dev Ethernet54 IP_ROUTE_CMD_TMPL = "config route {0} prefix {1} {2}" # add new routes ('replace' works even if old route exists if nh_str != "": exec_cmd = IP_ROUTE_CMD_TMPL.format("add", pkey_ar[1], nh_str) ret_val = util_utl.utl_execute_cmd(exec_cmd) else: # delete all old routes exec_cmd = IP_ROUTE_CMD_TMPL.format("del", pkey_ar[1], "") ret_val = util_utl.utl_execute_cmd(exec_cmd) return ret_val
def interface_restore_port_setting(db, port_name): # need to restore the admin status like teammgr # TODO: restore mtu ??? adm_val = interface_db_inf_status_get(db, port_name, PORT_ADMIN_STATUS, FILL_INFO_PORT) if adm_val and adm_val == 'up': exec_cmd = 'ip link set dev %s up' % port_name util_utl.utl_execute_cmd(exec_cmd)
def interface_remove_all_mbr_for_pc(db, pc_name): exec_cmd = 'teamdctl %s config dump actual' % pc_name (is_ok, output) = util_utl.utl_get_execute_cmd_output(exec_cmd) if is_ok: pc_cfg = json.loads(output) if "ports" in pc_cfg: for port in pc_cfg["ports"]: exec_cmd = TEAMD_CFG_PORT_CMD_TMPL.format( pc_name, 'remove', port) util_utl.utl_execute_cmd(exec_cmd) interface_restore_port_setting(db, port)
def nwi_pf_set_rule(root_yph, pkey_ar, val, is_create, disp_args): #pdb.set_trace() # # priority => RULE_MAX_PRI - sequence-id # # TODO: check policy type and action ??? try: acl_cfgs = {} if os.path.exists(ACL_JSON_FILE): with open(ACL_JSON_FILE) as infile: acl_cfgs = json.load(infile) if "acl" not in acl_cfgs: acl_cfgs["acl"] = {"acl-sets": {"acl-set": {}}} rules = SonicAcl.AclRule.FromString(val) for rule in rules.acl_rule_list: table_name = rule.acl_table_name.replace(" ", "_").replace( "-", "_").upper().encode('ascii') rule_name = rule.rule_name.replace(" ", "_").replace( "-", "_").upper().encode('ascii') data = rule.acl_rule_list if table_name not in acl_cfgs["acl"]["acl-sets"]["acl-set"]: acl_cfgs["acl"]["acl-sets"]["acl-set"][table_name] = {} table_cfgs = acl_cfgs["acl"]["acl-sets"]["acl-set"][table_name] if "acl-entries" not in table_cfgs: table_cfgs["acl-entries"] = {"acl-entry": {}} rules = table_cfgs["acl-entries"]["acl-entry"] rules[rule_name] = { "config": { "sequence-id": data.priority.value }, "actions": { "config": { "forwarding-action": str(AclPacketAction(data.packet_action)) } }, "ip": { "config": { "protocol": str(IPProtocol(data.ip_protocol.value)), "destination-ip-address": data.dst_ip.value, } } } with open(ACL_JSON_FILE, 'w') as outfile: json.dump(acl_cfgs, outfile) util_utl.utl_execute_cmd( "acl-loader update full {}".format(ACL_JSON_FILE)) except: return False return True
def interface_destroy_pc(pc_name, is_force=False): # teammgrd will destroy pc when pc is removed from db if not IS_NEW_TEAMMGRD or is_force: exec_cmd = 'docker exec teamd teamd -k -t %s' % pc_name return util_utl.utl_execute_cmd(exec_cmd) return True
def sonic_set_sonic_db(root_yph, pkey_ar, val, is_create, disp_args): if util_utl.utl_is_flag_on(util_utl.TAG_SKIP_QOS): for tbl in SONIC_QOS_TBL: if tbl in val: util_utl.utl_log('SKIP QOS: ' + val) return True exec_cmd = 'sonic-cfggen -a \'%s\' --write-to-db' % val ret_val = util_utl.utl_execute_cmd(exec_cmd) return ret_val
def nwi_pf_delete_policy(root_yph, pkey_ar, disp_args): try: table_name = pkey_ar[0].replace(" ", "_").replace( "-", "_").upper().encode('ascii') if os.path.exists(ACL_JSON_FILE): with open(ACL_JSON_FILE) as infile: acl_cfgs = json.load(infile) if "acl" in acl_cfgs and table_name in acl_cfgs["acl"]["acl-sets"][ "acl-set"]: del acl_cfgs["acl"]["acl-sets"]["acl-set"][table_name] with open(ACL_JSON_FILE, 'w') as outfile: json.dump(acl_cfgs, outfile) util_utl.utl_execute_cmd( "acl-loader update full {}".format(ACL_JSON_FILE)) disp_args.cfgdb.set_entry(util_utl.CFGDB_TABLE_NAME_ACL, table_name, None) except: return False return True
def sonic_set_one_mac_swss(mac_cfg): try: mac_port = None if 'port' not in mac_cfg else mac_cfg['port'] mac_trgt = mac_cfg['mac'] vlan = int(mac_cfg['vlan']) mac_mode = "SET" if 'mode' not in mac_cfg else mac_cfg['mode'].upper() except: return False conf = SWSS_CFG_TMPL_FDB % (vlan, mac_trgt.replace( ':', '-'), mac_port, mac_mode) exec_cmd = "echo '%s' | (docker exec -i swss bash -c 'cat > /tmp/fdb.json')" \ % (conf) if not util_utl.utl_execute_cmd(exec_cmd): return False exec_cmd = 'docker exec -i swss swssconfig /tmp/fdb.json' if not util_utl.utl_execute_cmd(exec_cmd): return False return True
def sys_set_ntp_server(root_yph, pkey_ar, val, is_create, disp_args): #pdb.set_trace() try: cfg_info = {"address": ""} if val == "" else eval(val) if cfg_info["address"] == "": ntp_cfg = None else: ntp_cfg = {} except: return False disp_args.cfgdb.mod_entry(util_utl.CFGDB_TABLE_NAME_NTP, pkey_ar[0], ntp_cfg) # restart the ntpd to make new config take effect util_utl.utl_execute_cmd("/usr/bin/ntp-config.sh") return True
def interface_set_cfg_enabled(oc_yph, pkey_ar, val, is_create, disp_args): # not support create if is_create: return False tbl = None if pkey_ar[0].startswith("Ethernet"): tbl = "PORT" elif pkey_ar[0].startswith("PortChannel"): tbl = "PORTCHANNEL" if IS_NEW_TEAMMGRD and tbl: # only need to modify db val = ["down", "up"][val.upper() == "TRUE"] disp_args.cfgdb.mod_entry(tbl, pkey_ar[0], {"admin_status": val}) else: exec_cmd = 'ifconfig %s %s' % (pkey_ar[0], ["down", "up" ][val.upper() == "TRUE"]) util_utl.utl_execute_cmd(exec_cmd) return True
def interface_set_cfg_name_pc(oc_yph, pkey_ar, is_create, disp_args): set_cmd = CFG_PC_CMD_TMPL % (pkey_ar[0], ["null", "{}"][is_create]) oc_infs = oc_yph.get("/interfaces")[0] #pdb.set_trace() ret_val = False if is_create: # need to write to db first to let other app start working if util_utl.utl_execute_cmd(set_cmd): interface_create_pc(pkey_ar[0]) oc_infs.interface.add(pkey_ar[0]) ret_val = True else: oc_infs.interface.delete(pkey_ar[0]) interface_remove_all_mbr_for_pc(disp_args.appdb, pkey_ar[0]) interface_destroy_pc(pkey_ar[0]) # remove port channel in db last to let other app finish jobs ret_val = util_utl.utl_execute_cmd(set_cmd) return ret_val
def interface_destroy_pc_by_teammgrd(pc_name): LOOP_CNT = 10 exec_cmd = "teamdctl %s state" % pc_name # wait for the pc created by teammgrd to show up for idx in range(LOOP_CNT): time.sleep(1) if util_utl.utl_execute_cmd(exec_cmd): break return interface_destroy_pc(pc_name, True)
def interface_create_pc(pc_name): global MY_MAC_ADDR if IS_NEW_TEAMMGRD: interface_destroy_pc_by_teammgrd(pc_name) # re-create the pc (static trunk) pc_cfg = '{"device":"%s","hwaddr":"%s","runner":{"active":"true","name":"%s"}}' % ( pc_name, MY_MAC_ADDR, TEAMD_CONF_RUNNER) exec_cmd = "docker exec teamd bash -c '/usr/bin/teamd -r -t %s -c '\\''%s'\\'' -L /var/warmboot/teamd/ -d'" % ( pc_name, pc_cfg) return util_utl.utl_execute_cmd(exec_cmd) else: # populate create info to teamd conf = TEAMD_CONF_TMPL % (pc_name, MY_MAC_ADDR, TEAMD_CONF_RUNNER) exec_cmd = "echo '%s' | (docker exec -i teamd bash -c 'cat > %s/%s.conf')" \ % (conf, TEAMD_CONF_PATH, pc_name) if not util_utl.utl_execute_cmd(exec_cmd): return False exec_cmd = 'docker exec teamd teamd -d -f %s/%s.conf' % ( TEAMD_CONF_PATH, pc_name) if not util_utl.utl_execute_cmd(exec_cmd): return False
def interface_set_nbr_v4(oc_yph, pkey_ar, val, is_create, disp_args): # pdb.set_trace() try: nbr_cfg = {} if val == "" else eval(val) lladdr_cmd = "" if "link-layer-address" not in nbr_cfg else \ "lladdr %s" % nbr_cfg["link-layer-address"] except: return False exec_cmd = "ip neigh {0} {1} {2} dev {3}".format( ['replace', 'del'][lladdr_cmd == ""], pkey_ar[1], lladdr_cmd, pkey_ar[0]) ret_val = util_utl.utl_execute_cmd(exec_cmd) return ret_val
def restart_dhcp_relay(): return util_utl.utl_execute_cmd("systemctl restart dhcp_relay")
def sonic_set_sonic_db(root_yph, pkey_ar, val, is_create, disp_args): exec_cmd = 'sonic-cfggen -a \'%s\' --write-to-db' % val ret_val = util_utl.utl_execute_cmd(exec_cmd) return ret_val