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 ProtocolHeaderAttribute import *
15 from ProtocolHeaderField import *
16 from InputFormat import *
17 import ExpressionConverter
22 def __init__(self, node):
26 self.attributeDict = {}
31 for field in self.node.fields:
32 phf = ProtocolHeaderField(field.Size, field.DefaultValue, None, field)
33 self.fields.append(phf)
34 if field.Name != "reserved":
35 self.fieldDict[field.Name] = phf
37 for attr in self.node.attributes:
38 pha = ProtocolHeaderAttribute(attr.Size, attr.DefaultValue, attr)
39 self.attributes.append(pha)
40 self.attributeDict[attr.Name] = pha
49 return self.attributes
51 def setField(self, name, expression, auto):
52 if name == "reserved":
55 if name not in self.fieldDict:
58 field = self.fieldDict[name]
60 if field.UpdateValue(expression, auto):
66 def SetField(self, name, expression):
67 return self.setField(name, expression, False)
69 def SetFieldAuto(self, name, expression):
70 return self.setField(name, expression, True)
72 def SetAttribute(self, name, expression):
73 if name not in self.attributeDict:
75 attr = self.attributeDict[name]
77 return attr.UpdateValue(expression)
79 def SetMask(self, name, expression):
80 if name not in self.fieldDict:
82 field = self.fieldDict[name]
84 return field.UpdateMask(expression)
86 def resolveOptional(self, condition):
90 tokens = condition.split("|")
96 result |= self.resolveOptional(token)
100 tokens = condition.split("&")
106 result &= self.resolveOptional(token)
113 if "!=" in tokens[0]:
114 index = tokens[0].find("!=")
115 key = tokens[0][:index].strip()
116 value = tokens[0][index + 1 :].strip()
117 elif "=" in tokens[0]:
118 index = tokens[0].find("=")
119 key = tokens[0][:index].strip()
120 value = tokens[0][index + 1 :].strip()
124 if key not in self.fieldDict:
127 f = self.fieldDict[key]
128 return ExpressionConverter.Equal(f.Value, value)
130 def resolveSize(self, exp):
135 offset = exp.find("<<")
136 key = exp[0:offset].strip()
137 shift = int(exp[offset + 2 :].strip())
139 if self.fieldDict.has_key(key):
140 field = self.fieldDict[key]
141 _, u16 = ExpressionConverter.ToNum(field.Value)
147 if self.attributeDict.has_key(key):
148 attr = self.attributeDict[key]
149 _, u16 = ExpressionConverter.ToNum(attr.Value)
161 self.resolveAllSize()
163 for phf in self.fields:
164 if phf.Field.IsAutoIncrease:
165 autoIncreases.append(phf)
166 if phf.Field.IsIncreaseLength and self.resolveOptional(phf.Field.Optional):
167 increaseHeaders.append(phf)
169 for f1 in autoIncreases:
170 for f2 in increaseHeaders:
172 ExpressionConverter.IncreaseValue(f1.Value, f2.Size >> 3), True
175 def resolveAllSize(self):
176 for phf in self.fields:
177 if phf.Field.Optional != None and not self.resolveOptional(
182 if phf.Field.VariableSize != None:
183 size = self.resolveSize(phf.Field.VariableSize)
185 size = phf.Field.Size
191 for field in self.fields:
196 def AppendAuto(self, size):
197 for phf in self.fields:
198 if not phf.Field.IsAutoIncrease:
201 phf.UpdateValue(ExpressionConverter.IncreaseValue(phf.Value, size), True)
203 def getField(self, name):
204 if not self.fieldDict.has_key(name):
206 field = self.fieldDict[name]
210 def getAttribute(self, name):
211 if not self.attributeDict.has_key(name):
214 return self.attributeDict[name].Value
216 def GetValue(self, name):
217 result = self.getField(name)
220 return self.getAttribute(name)
224 def appendNum(self, big, exp, size):
227 _, num = ExpressionConverter.ToNum(exp)
229 print("Invalid byte expression")
233 num = num & ((1 << size) - 1)
238 def appendUInt64(self, big, exp, size):
241 _, u64 = ExpressionConverter.ToNum(exp)
243 print("Invalid UInt32 expression")
248 u64 = u64 & ((1 << size) - 1)
253 def appendIPv4(self, big, exp):
256 _, ipv4 = ExpressionConverter.ToIPv4Address(exp)
258 print("Inavalid IPv4 Address")
261 for i in range(len(ipv4)):
267 def appendIPv6(self, big, exp):
270 _, ipv6 = ExpressionConverter.ToIPv6Address(exp)
272 print("Inavalid IPv6 Address")
281 def appendMAC(self, big, exp):
284 _, mac = ExpressionConverter.ToMacAddress(exp)
286 print("Inavalid MAC Address")
295 def appendByteArray(self, big, exp, size):
296 array = bytes(size >> 3)
298 _, array = ExpressionConverter.ToByteArray(exp)
300 print("Invalid byte array")
303 for i in range(size >> 3):
310 def append(self, big, phf):
311 bigVal = big["bigVal"]
312 bigMsk = big["bigMsk"]
314 if phf.Field.IsReserved:
317 big.update(bigVal=bigVal, bigMsk=bigMsk)
323 phf.Field.Format == InputFormat.u8
324 or phf.Field.Format == InputFormat.u16
325 or phf.Field.Format == InputFormat.u32
327 bigVal = self.appendNum(bigVal, phf.Value, size)
328 bigMsk = self.appendNum(bigMsk, phf.Mask, size)
330 elif phf.Field.Format == InputFormat.u64:
331 bigVal = self.appendUInt64(bigVal, phf.Value, size)
332 bigMsk = self.appendUInt64(bigMsk, phf.Mask, size)
334 elif phf.Field.Format == InputFormat.ipv4:
335 bigVal = self.appendIPv4(bigVal, phf.Value)
336 bigMsk = self.appendIPv4(bigMsk, phf.Mask)
338 elif phf.Field.Format == InputFormat.ipv6:
339 bigVal = self.appendIPv6(bigVal, phf.Value)
340 bigMsk = self.appendIPv6(bigMsk, phf.Mask)
342 elif phf.Field.Format == InputFormat.mac:
343 bigVal = self.appendMAC(bigVal, phf.Value)
344 bigMsk = self.appendMAC(bigMsk, phf.Mask)
346 elif phf.Field.Format == InputFormat.bytearray:
347 bigVal = self.appendByteArray(bigVal, phf.Value, size)
348 bigMsk = self.appendByteArray(bigMsk, phf.Mask, size)
351 print("Invalid input format")
353 big.update(bigVal=bigVal, bigMsk=bigMsk)
357 big = {"bigVal": 0, "bigMsk": 0}
360 for phf in self.fields:
364 big, bits = self.append(big, phf)
371 bigVal = big["bigVal"]
372 bigMsk = big["bigMsk"]
375 byteList1.append(bigVal & 0xFF)
376 byteList2.append(bigMsk & 0xFF)
383 buffer = copy.deepcopy(byteList1)
384 mask = copy.deepcopy(byteList2)