""" Copyright (c) 2014-2018 Alex Forencich Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from myhdl import * import axis_ep import struct import zlib class EthFrame(object): def __init__(self, payload=b'', eth_dest_mac=0, eth_src_mac=0, eth_type=0, eth_fcs=None): self._payload = axis_ep.AXIStreamFrame() self.eth_dest_mac = eth_dest_mac self.eth_src_mac = eth_src_mac self.eth_type = eth_type self.eth_fcs = eth_fcs if type(payload) is dict: self.payload = axis_ep.AXIStreamFrame(payload['eth_payload']) self.eth_dest_mac = payload['eth_dest_mac'] self.eth_src_mac = payload['eth_src_mac'] self.eth_type = payload['eth_type'] self.eth_fcs = payload['eth_fcs'] if type(payload) is bytes: payload = bytearray(payload) if type(payload) is bytearray or type(payload) is axis_ep.AXIStreamFrame: self.payload = axis_ep.AXIStreamFrame(payload) if type(payload) is EthFrame: self.payload = axis_ep.AXIStreamFrame(payload.payload) self.eth_dest_mac = payload.eth_dest_mac self.eth_src_mac = payload.eth_src_mac self.eth_type = payload.eth_type self.eth_fcs = payload.eth_fcs @property def payload(self): return self._payload @payload.setter def payload(self, value): self._payload = axis_ep.AXIStreamFrame(value) def calc_fcs(self): frame = self.build_axis().data return zlib.crc32(bytes(frame)) & 0xffffffff def update_fcs(self): self.eth_fcs = self.calc_fcs() def build_axis(self): data = b'' data += struct.pack('>Q', self.eth_dest_mac)[2:] data += struct.pack('>Q', self.eth_src_mac)[2:] data += struct.pack('>H', self.eth_type) data += self.payload.data return axis_ep.AXIStreamFrame(data) def build_axis_fcs(self): if self.eth_fcs is None: self.update_fcs() data = self.build_axis().data data += struct.pack('Q', b'\x00\x00'+data[0:6])[0] self.eth_src_mac = struct.unpack('>Q', b'\x00\x00'+data[6:12])[0] self.eth_type = struct.unpack('>H', data[12:14])[0] data = data[14:] self.payload = axis_ep.AXIStreamFrame(data) def parse_axis_fcs(self, data): self.parse_axis(data) data = self.payload.data self.payload = axis_ep.AXIStreamFrame(data[:-4]) self.eth_fcs = struct.unpack('