1 # Copyright (c) 2022 Intel and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 from vpp_papi.vpp_papi import VPPApiClient
20 # Get VPP json API file directory
21 CLIENT_ID = "Vppclient"
23 os.path.abspath("../..") + "/build-root/install-vpp-native/vpp/share/vpp/api/core"
25 VPP_JSON_DIR_PLUGIN = (
26 os.path.abspath("../..")
27 + "/build-root/install-vpp-native/vpp/share/vpp/api/plugins"
29 API_FILE_SUFFIX = "*.api.json"
32 def load_json_api_files(suffix=API_FILE_SUFFIX):
34 json_dir = VPP_JSON_DIR
35 for root, dirnames, filenames in os.walk(json_dir):
36 for filename in fnmatch.filter(filenames, suffix):
37 jsonfiles.append(os.path.join(json_dir, filename))
38 json_dir = VPP_JSON_DIR_PLUGIN
39 for root, dirnames, filenames in os.walk(json_dir):
40 for filename in fnmatch.filter(filenames, suffix):
41 jsonfiles.append(os.path.join(json_dir, filename))
43 raise RuntimeError("Error: no json api files found")
45 print("load json api file done")
49 def connect_vpp(jsonfiles):
50 vpp = VPPApiClient(apifiles=jsonfiles)
51 r = vpp.connect("CLIENT_ID")
52 print("VPP api opened with code: %s" % r)
62 opts, args = getopt.getopt(
77 except getopt.GetoptError:
79 "flow_create.py --add|del|show -f <file> -p <pattern> -a <actions> -i <interface> -I <flow-index>"
85 "flow_create.py --add|del|show -f <file> -p <pattern> -a <actions> -i <interface> -I <flow-index>"
94 elif opt in ("-f", "--file"):
97 elif opt in ("-p", "--pattern") and not file_flag:
99 elif opt in ("-a", "--actions"):
101 elif opt in ("-i", "--interface"):
103 elif opt in ("-I", "--flow-index"):
106 if operation == None:
107 print("Error: Please choose the operation: add or del")
110 if operation == "show":
112 result = packetforge.Forge(pattern, actions, False, True)
114 result = packetforge.Forge(json_file, actions, True, True)
115 return result, None, operation, None, None
117 # Python API need json definitions to interpret messages
118 vpp = connect_vpp(load_json_api_files())
120 # set inteface states
121 vpp.api.sw_interface_set_flags(sw_if_index=int(iface), flags=1)
123 if operation == "add":
125 result = packetforge.Forge(pattern, actions, False, False)
127 result = packetforge.Forge(json_file, actions, True, False)
128 return result, int(iface), operation, None, vpp
129 elif operation == "del":
130 return None, int(iface), operation, int(flow_index), vpp
133 if __name__ == "__main__":
134 # Parse the arguments
135 my_flow, iface, operation, del_flow_index, vpp = Main(sys.argv[1:])
137 # if operation is show, just show spec and mask, then exit
138 if operation == "show":
142 if operation == "add":
144 rv = vpp.api.flow_add_v2(flow=my_flow)
149 rv = vpp.api.flow_enable(flow_index=flow_index, hw_if_index=iface)
151 # if enable flow fail, delete added flow
153 print("Error: enable flow failed, delete flow")
154 rv = vpp.api.flow_del(flow_index=flow_index)
157 elif operation == "del":
159 rv = vpp.api.flow_disable(flow_index=del_flow_index, hw_if_index=iface)
162 print("Error: disable flow failed")
167 rv = vpp.api.flow_del(flow_index=del_flow_index)
171 # python flow_create.py --add -p "mac()/ipv4(src=1.1.1.1,dst=2.2.2.2)/udp()" -a "redirect-to-queue 3" -i 1
172 # python flow_create.py --del -i 1 -I 0
173 # python flow_create.py --show -p "mac()/ipv4(src=1.1.1.1,dst=2.2.2.2)/udp()"