1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# Copyright (c) 2022 Intel and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from vpp_papi.vpp_papi import VppEnum
from ParseGraph import *
from Path import *
import json
import re
import os
parsegraph_path = os.getcwd() + "/parsegraph"
def Forge(pattern, actions, file_flag):
pg = ParseGraph.Create(parsegraph_path)
if pg == None:
print("error: create parsegraph failed")
return None
if not file_flag:
token = ParsePattern(pattern)
if token == None:
return None
else:
if not os.path.exists(pattern):
print("error: file not exist '%s' " % (pattern))
return
f = open(pattern, "r", encoding="utf-8")
token = json.load(f)
if "actions" in token:
actions = token["actions"]
path = Path.Create(token)
if path == None:
print("error: path not exit")
return None
result = pg.Forge(path)
if result == None:
print("error: result not available")
return None
spec, mask = GetBinary(result.ToJSON())
# create generic flow
my_flow = {
"type": VppEnum.vl_api_flow_type_v2_t.FLOW_TYPE_GENERIC_V2,
"flow": {
"generic": {
"pattern": {"spec": bytes(spec.encode()), "mask": bytes(mask.encode())}
}
},
}
# update actions entry
my_flow = GetAction(actions, my_flow)
return my_flow
def GetAction(actions, flow):
if len(actions.split(" ")) > 1:
type = actions.split(" ")[0]
else:
type = actions
if type == "mark":
flow.update(
{
"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_MARK_V2,
"mark_flow_id": int(actions.split(" ")[1]),
}
)
elif type == "next-node":
flow.update(
{
"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_REDIRECT_TO_NODE_V2,
"redirect_node_index": int(actions.split(" ")[1]),
}
)
elif type == "buffer-advance":
flow.update(
{
"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_BUFFER_ADVANCE_V2,
"buffer_advance": int(actions.split(" ")[1]),
}
)
elif type == "redirect-to-queue":
flow.update(
{
"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_REDIRECT_TO_QUEUE_V2,
"redirect_queue": int(actions.split(" ")[1]),
}
)
elif type == "rss":
flow.update({"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_RSS_V2})
elif type == "rss-queues":
queue_end = int(actions.split(" ")[-1])
queue_start = int(actions.split(" ")[-3])
flow.update(
{
"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_RSS_V2,
"queue_index": queue_start,
"queue_num": queue_end - queue_start + 1,
}
)
elif type == "drop":
flow.update({"actions": VppEnum.vl_api_flow_action_v2_t.FLOW_ACTION_DROP_V2})
return flow
def GetBinary(flow_info):
spec = "".join(flow_info["Packet"])
mask = "".join(flow_info["Mask"])
return spec, mask
def ParseFields(item):
# get protocol name
prot = item.split("(")[0]
stack = {"header": prot}
# get fields contents
fields = re.findall(r"[(](.*?)[)]", item)
if not fields:
print("error: invalid pattern")
return None
if fields == [""]:
return stack
stack.update({"fields": []})
return ParseStack(stack, fields[0].split(","))
def GetMask(item):
if "format" in item:
format = item["format"]
if format == "mac":
mask = "ff.ff.ff.ff.ff.ff"
elif format == "ipv4":
mask = "255.255.255.255"
elif format == "ipv6":
mask = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
return mask
if "size" in item:
mask = str((1 << int(item["size"])) - 1)
else:
print("mask error")
return mask
# parse protocol headers and its fields. Available fields are defined in corresponding nodes.
def ParseStack(stack, fields):
prot = stack["header"]
node_path = parsegraph_path + "/nodes/" + prot + ".json"
if not os.path.exists(node_path):
print("error file not exist '%s' " % (node_path))
return None
f = open(node_path, "r", encoding="utf-8")
nodeinfo = json.load(f)
for field in fields:
fld_name = field.split("=")[0].strip()
fld_value = (
field.split("=")[-1].strip() if (len(field.split("=")) >= 2) else None
)
for item in nodeinfo["layout"]:
if fld_name == item["name"]:
mask = GetMask(item)
stack["fields"].append(
{"name": fld_name, "value": fld_value, "mask": mask}
)
break
if not stack["fields"]:
print("warning: invalid field '%s'" % (fld_name))
return None
return stack
def ParsePattern(pattern):
# create json template
json_tmp = {"type": "path", "stack": []}
items = pattern.split("/")
for item in items:
stack = ParseFields(item)
if stack == None:
return None
json_tmp["stack"].append(stack)
return json_tmp
|