def testFind(self): a = BitArray('0b10101010, 0xabcd, 0b10101010, 0x0') p, = a.find('0b10101010', bytealigned=False) self.assertEqual(p, 4) p, = a.find('0b10101010', start=4, bytealigned=False) self.assertEqual(p, 4) p, = a.find('0b10101010', start=5, bytealigned=False) self.assertEqual(p, 22)
def compress(data, windowBits, lengthBits): maxWindowLength = 2**windowBits - 1 bufferLength = 2**lengthBits - 1 buffer = data[:bufferLength] substring = buffer compressed = BitArray() window = BitArray('') #constants in the case that a match is not found zeroPos = Bits(uint=0, length=windowBits) zeroLength = Bits(uint=0, length=lengthBits) bufferPos = 0 maxLength = len(data) while ((bufferPos) < maxLength): bufferExtend = min(bufferPos + bufferLength, maxLength) buffer = data[bufferPos:bufferExtend] bufferStepper = len(buffer) tripletAdded = False while bufferStepper > 0 and not tripletAdded: substring = buffer[0:bufferStepper] if (window.find(substring) != ()): position = len(window) - int(window.find(substring)[0]) length = len(substring) nextCharIndex = bufferPos + length if nextCharIndex > len(data) - 1: nextCharIndex -= 1 substring = substring[:-1] length = len(substring) nextChar = data[nextCharIndex:nextCharIndex + 1] bitsPosition = Bits(uint=position, length=windowBits) bitsLength = Bits(uint=length, length=lengthBits) compressedTriplet = bitsPosition + bitsLength + nextChar substring += nextChar length += 1 tripletAdded = True elif len(substring) == 1: length = 1 compressedTriplet = zeroPos + zeroLength + substring tripletAdded = True bufferStepper -= 1 bufferPos += length window += substring if len(window) > maxWindowLength: startIndex = len(window) - maxWindowLength window = window[startIndex:] compressed += compressedTriplet return compressed
def run_length(bs: BitArray, word_size: int) -> int: ''' Args: bs: the bits to analyze. word_size: the WAH word size. Returns: the number of run sections at the beginning of the bitstring. Note that a single word can encode up to ``2**(word_size - 2) - 1`` runs. If the run is too long to be encoded in a single word, the maximum possible length is returned. ''' if len(bs) == 0: return 0 section_size = word_size - 1 toggle_bit = BitArray(bool=not bs[0]) bit_idx = bs.find(toggle_bit) run_bits = bit_idx[0] if bit_idx else len(bs) if run_bits < section_size: return 0 run_words = run_bits // section_size max_run_words = 2**(section_size - 1) - 1 return min(max_run_words, run_words)
def main(top_block_cls=top_block, options=None): tb = top_block_cls() tb.start() tb.wait() print("Looking for flag") stream = BitArray() with open(os.getenv("DIR", "/data") + os.sep + "output.txt", "rb") as f: byte = f.read(1) while byte: if byte == b'\x01': stream.append('0b1') else: stream.append('0b0') byte = f.read(1) # look for the start of the flag in the stream of bits pos = stream.find('0x666c6167') if len(pos) > 0: stream <<= pos[0] bitlen = len(stream) print(stream[0:bitlen / 8 * 8].bytes) else: print("Did not find the flag")
class IndexPool(object): def __init__(self, max_entries, offset): self.max_entries = max_entries self.offset = offset self.indices = BitArray(self.max_entries) def get_next(self): try: _pos = self.indices.find('0b0') self.indices.set(1, _pos) return self.offset + _pos[0] except IndexError: log.info("exception-fail-to-allocate-id-all-bits-in-use") return None def release(self, index): index -= self.offset _pos = (index,) try: self.indices.set(0, _pos) except IndexError: log.info("bit-position-{}-out-of-range".format(index)) #index or multiple indices to set all of them to 1 - need to be a tuple def pre_allocate(self, index): if(isinstance(index, tuple)): _lst = list(index) for i in range(len(_lst)): _lst[i] -= self.offset index = tuple(_lst) self.indices.set(1, index)
class RtpPayloadMp3: # En principio para MP3 def setAudio(self, filePath): with open (filePath, "rb") as file: bytes = file.read(); self.bits = BitArray(bytes).bin; riff = BitArray(hex='0x52494646').bin self.headerIndex = self.bits.find(riff) def takeMp3Frame(self): frameSize = self.bits[self.headerIndex+32:self.headerIndex+64]
def testNotByteAligned(self): bitstring.settings.bytealigned = False a = BitArray('0x00 ff 0f f') l = list(a.findall('0xff')) self.assertEqual(l, [8, 20]) p = a.find('0x0f')[0] self.assertEqual(p, 4) p = a.rfind('0xff')[0] self.assertEqual(p, 20) s = list(a.split('0xff')) self.assertEqual(s, ['0x00', '0xff0', '0xff']) a.replace('0xff', '') self.assertEqual(a, '0x000')
def testByteAligned(self): bitstring.bytealigned = True a = BitArray("0x00 ff 0f f") l = list(a.findall("0xff")) self.assertEqual(l, [8]) p = a.find("0x0f")[0] self.assertEqual(p, 16) p = a.rfind("0xff")[0] self.assertEqual(p, 8) s = list(a.split("0xff")) self.assertEqual(s, ["0x00", "0xff0ff"]) a.replace("0xff", "") self.assertEqual(a, "0x000ff")
def testByteAligned(self): bitstring.settings.bytealigned = True a = BitArray('0x00 ff 0f f') l = list(a.findall('0xff')) self.assertEqual(l, [8]) p = a.find('0x0f')[0] self.assertEqual(p, 16) p = a.rfind('0xff')[0] self.assertEqual(p, 8) s = list(a.split('0xff')) self.assertEqual(s, ['0x00', '0xff0ff']) a.replace('0xff', '') self.assertEqual(a, '0x000ff')
def testByteAligned(self): bitstring.bytealigned = True a = BitArray('0x00 ff 0f f') l = list(a.findall('0xff')) self.assertEqual(l, [8]) p = a.find('0x0f')[0] self.assertEqual(p, 16) p = a.rfind('0xff')[0] self.assertEqual(p, 8) s = list(a.split('0xff')) self.assertEqual(s, ['0x00', '0xff0ff']) a.replace('0xff', '') self.assertEqual(a, '0x000ff')
def testNotByteAligned(self): bitstring.bytealigned = False a = BitArray('0x00 ff 0f f') l = list(a.findall('0xff')) self.assertEqual(l, [8, 20]) p = a.find('0x0f')[0] self.assertEqual(p, 4) p = a.rfind('0xff')[0] self.assertEqual(p, 20) s = list(a.split('0xff')) self.assertEqual(s, ['0x00', '0xff0', '0xff']) a.replace('0xff', '') self.assertEqual(a, '0x000')
def test_dirty_bit_pos(self): ''' Test ``bbc.dirty_bit_pos()`` against all possible bytes. ''' for bits in it.product(*it.repeat(['0', '1'], 8)): byte = BitArray(bin=''.join(bits)) if byte.count(1) == 1: # offset byte found self.assertEqual(bbc.dirty_bit_pos(byte), byte.find('0b1')[0]) else: # non-offset byte found self.assertEqual(bbc.dirty_bit_pos(byte), -1)
class RtpPayloadMp3: # En principio para MP3 def __init__(self, file_path): with open(file_path, "rb") as file: bytes = file.read() self.bits = BitArray(bytes).bin self.header_index = self.bits.find('11111111111') def _take_mp3_frame(self): header = self.bits[self.header_index:self.header_index + 32] if not header: return None frame_sync = header[0:11] version = header[11:13] layer = header[13:15] protection = header[15] bitrate = header[16:20] sampling = header[20:22] padding = header[22] private = header[23] channel = header[24:26] modeext = header[26:28] copyright = header[28] original = header[29] emphasis = header[30:] # Se asume que es version 1 layer 3 (mp3) if version == '11': bps = _bps_dict[bitrate] sample_rate = _sample_rate_dict[sampling] elif version == '00': bps = _bps_dict_2[bitrate] sample_rate = _sample_rate_dict_25[sampling] elif version == '10': bps = _bps_dict_2[bitrate] sample_rate = _sample_rate_dict_2[sampling] else: print("version", version) # 144 * bit rate / sample rate * 8 (el 144 es en bytes) frame_length = int(144 * 8 * (bps / sample_rate)) # tiempo por frame en milisegundos self.frameTimeMs = int(144 / sample_rate * 1000 * 8) next_mp3_header_index = self.header_index + frame_length self.frame = self.bits[self.header_index:next_mp3_header_index] self.header_index = next_mp3_header_index return 1
def get_gaps(bs: BitArray): ''' Args: bs: the bits to check for gaps. Returns: a tuple ``(tail, gaps)``, where ``tail`` is ``bs`` without the leading gaps and ``gaps`` is the number of gap bytes to encode. ''' bit_idx = bs.find('0b1') gap_bits = bit_idx[0] if bit_idx else len(bs) gap_max = all_bits(max_gap_bits) if gap_bits < bits_per_byte: return bs, 0 # if the number of gaps found is greater than the amount that can be # stored in an atom, only take what can be stored gaps = min(gap_max, gap_bits // bits_per_byte) return bs[gaps * bits_per_byte:], gaps
class ESXVlans(object): """The vlan object""" def __init__(self, esx_session): self._vlan_id_bits = BitArray(MAX_VLAN_ID + 1) self._init_vlans(esx_session) #end __init__() def _init_vlans(self, esx_session): session = esx_session host_mor = vm_util.get_host_ref(session) port_grps_on_host_ret = session._call_method(vim_util, "get_dynamic_property", host_mor, "HostSystem", "config.network.portgroup") if not port_grps_on_host_ret: return port_grps_on_host = port_grps_on_host_ret.HostPortGroup for p_gp in port_grps_on_host: if p_gp.spec.vlanId >= 0: self._vlan_id_bits[p_gp.spec.vlanId] = 1 #end _init_vlans() def alloc_vlan(self): vid = self._vlan_id_bits.find('0b0') if vid: self._vlan_id_bits[vid[0]] = 1 return vid[0] return INVALID_VLAN_ID #end alloc_vlan() def free_vlan(self, vlan_id): if vlan_id < 0 or vlan_id > MAX_VLAN_ID: return self._vlan_id_bits[vlan_id] = 0
class RtpPayloadMp3: # En principio para MP3 def setAudio(self, filePath): with open (filePath, "rb") as file: bytes = file.read(); self.bits = BitArray(bytes).bin; riff = BitArray(hex='0x52494646').bin self.headerIndex = self.bits.find(riff) def takeMp3Frame(self): frameSize = self.bits[self.headerIndex+32:self.headerIndex+64] if __name__== "__main__": with open ("archivo.mp3", "rb") as file: # Abro archivo en modo lectura y binario bytes = file.read(); bits = BitArray(bytes).bin; headerIndex = bits.find('11111111111'); #buscamos la cabecera takeFrame(bits, headerIndex); #while byte: #byte = '' es false #hago algo con el byte #numero = int.from_bytes(byte, byteorder='big'); #its = f'{numero:08b}' #convierte el numero a 8 bits #print(bits); #byte = file.read(1)
import wave from bitstring import BitArray f = wave.open('/Users/macbook/Desktop/HW5/[email protected]', 'rb') token = '' for x in (f.readframes(f.getnframes())[::2]): token = token + str(x & 1) s = BitArray(bin=token).tobytes().decode() s[s.find('COM402'):][0:52]
f = open(sys.argv[1],'r+b') f.seek(0,2) size = f.tell() print "[*] file size: %d" % size f.seek(0,0) print "[*] ReeeeeWWWWWWiiiiiNNNNNNND" fb = BitArray(f) index = fb.find('0xa1dcab8c47a9cf118ee400c00c205365',bytealigned=True) print "[*] found file properties GUID" print "[*] File properties GUID: %s" % fb[index[0]:(index[0]+128)] # index of minumum packet size in File Proprties header i_min_data_pkt_size = index[0] + 736 print "[*] Original Minimum Data Packet Size: %s" % fb[i_min_data_pkt_size:i_min_data_pkt_size+32].hex print "[*] Original Maximum Data Packet Size: %s" % fb[i_min_data_pkt_size+32:i_min_data_pkt_size+64].hex # Accroding to ASF standarad the minimum data size and the maximum data size should be equal print "[*] Changing Miniumum and Maximum Data packet size to 0" # changing the data packets in bit array
f = open(sys.argv[1], 'r+b') f.seek(0, 2) size = f.tell() print "[*] file size: %d" % size f.seek(0, 0) print "[*] ReeeeeWWWWWWiiiiiNNNNNNND" fb = BitArray(f) index = fb.find('0xa1dcab8c47a9cf118ee400c00c205365', bytealigned=True) print "[*] found file properties GUID" print "[*] File properties GUID: %s" % fb[index[0]:(index[0] + 128)] # index of minumum packet size in File Proprties header i_min_data_pkt_size = index[0] + 736 print "[*] Original Minimum Data Packet Size: %s" % fb[ i_min_data_pkt_size:i_min_data_pkt_size + 32].hex print "[*] Original Maximum Data Packet Size: %s" % fb[i_min_data_pkt_size + 32:i_min_data_pkt_size + 64].hex # Accroding to ASF standarad the minimum data size and the maximum data size should be equal print "[*] Changing Miniumum and Maximum Data packet size to 0"
class VirtualMachineInterfaceKM(DBBaseKM): _dict = {} obj_type = 'virtual_machine_interface' _ann_fq_name_to_uuid = {} ann_fq_name_key = ["kind", "name"] _fq_name_to_uuid = {} def __init__(self, uuid, obj_dict=None): self.uuid = uuid self.host_id = None self.virtual_network = None self.virtual_machine = None self.instance_ips = set() self.floating_ips = set() self.virtual_machine_interfaces = set() self.vlan_id = None self.vlan_bit_map = None self.security_groups = set() super(VirtualMachineInterfaceKM, self).__init__(uuid, obj_dict) obj_dict = self.update(obj_dict) self.add_to_parent(obj_dict) def update(self, obj=None): if obj is None: obj = self.read_obj(self.uuid) self.name = obj['fq_name'][-1] self.fq_name = obj['fq_name'] self.annotations = obj.get('annotations', None) self.build_fq_name_to_uuid(self.uuid, obj) # Cache bindings on this VMI. if obj.get('virtual_machine_interface_bindings', None): bindings = obj['virtual_machine_interface_bindings'] kvps = bindings.get('key_value_pair', None) for kvp in kvps or []: if kvp['key'] == 'host_id': self.host_id = kvp['value'] self.update_multiple_refs('instance_ip', obj) self.update_multiple_refs('floating_ip', obj) self.update_single_ref('virtual_network', obj) self.update_single_ref('virtual_machine', obj) self.update_multiple_refs('security_group', obj) self.update_multiple_refs('virtual_machine_interface', obj) # Update VMI properties. vlan_id = None if obj.get('virtual_machine_interface_properties', None): props = obj['virtual_machine_interface_properties'] # Property: Vlan ID. vlan_id = props.get('sub_interface_vlan_tag', None) # If vlan is configured on this interface, cache the appropriate # info. # # In nested mode, if the interface is a sub-interface, the vlan id # space is allocated and managed in the parent interface. So check to # see if the interface has a parent. If it does, invoke the appropriate # method on the parent interface for vlan-id management. if (vlan_id is not None or self.vlan_id is not None) and\ vlan_id is not self.vlan_id: # Vlan is configured on this interface. if DBBaseKM.is_nested(): # We are in nested mode. # Check if this interface has a parent. If yes, this is # is a sub-interface and the vlan-id should be managed on the # vlan-id space of the parent interface. parent_vmis = self.virtual_machine_interfaces for parent_vmi_id in parent_vmis: parent_vmi = VirtualMachineInterfaceKM.locate( parent_vmi_id) if not parent_vmi: continue if self.vlan_id is not None: parent_vmi.reset_vlan(self.vlan_id) if vlan_id is not None: parent_vmi.set_vlan(vlan_id) # Cache the Vlan-id. self.vlan_id = vlan_id else: # Nested mode is not configured. # Vlan-id is NOT managed on the parent interface. # Just cache and proceed. self.vlan_id = vlan_id return obj @classmethod def delete(cls, uuid): if uuid not in cls._dict: return obj = cls._dict[uuid] # If vlan-id is configured and if we are in nested mode, # free up the vlan-id which was claimed from the parent # interface. if obj.vlan_id and DBBaseKM.is_nested(): parent_vmi_ids = obj.virtual_machine_interfaces for parent_vmi_id in parent_vmi_ids: parent_vmi = VirtualMachineInterfaceKM.get(parent_vmi_id) if parent_vmi: parent_vmi.reset_vlan(obj.vlan_id) obj.update_multiple_refs('instance_ip', {}) obj.update_multiple_refs('floating_ip', {}) obj.update_single_ref('virtual_network', {}) obj.update_single_ref('virtual_machine', {}) obj.update_multiple_refs('security_group', {}) obj.update_multiple_refs('virtual_machine_interface', {}) obj.remove_from_parent() del cls._dict[uuid] def set_vlan(self, vlan): if vlan < MIN_VLAN_ID or vlan > MAX_VLAN_ID: return if not self.vlan_bit_map: self.vlan_bit_map = BitArray(MAX_VLAN_ID + 1) self.vlan_bit_map[vlan] = 1 def reset_vlan(self, vlan): if vlan < MIN_VLAN_ID or vlan > MAX_VLAN_ID: return if not self.vlan_bit_map: return self.vlan_bit_map[vlan] = 0 def alloc_vlan(self): if not self.vlan_bit_map: self.vlan_bit_map = BitArray(MAX_VLAN_ID + 1) vid = self.vlan_bit_map.find('0b0', MIN_VLAN_ID) if vid: self.set_vlan(vid[0]) return vid[0] return INVALID_VLAN_ID def get_vlan(self): return vlan_id
def element_calulator(Pulses): ch1 = [-1] ch2 = [-1] ch3 = [-1] ch4 = [-1] for arg in Pulses: if (arg.channel == 'ch1'): ch1.append(arg.start) ch1.append(arg.end) elif (arg.channel == 'ch2'): ch2.append(arg.start) ch2.append(arg.end) elif (arg.channel == 'ch3'): ch3.append(arg.start) ch3.append(arg.end) elif (arg.channel == 'ch4'): ch4.append(arg.start) ch4.append(arg.end) tot_length = max(max(ch1),max(ch2),max(ch3),max(ch4)) del ch1[0] del ch2[0] del ch3[0] del ch4[0] channel1 = BitArray(int=0, length=tot_length) channel2 = BitArray(int=0, length=tot_length) channel3 = BitArray(int=0, length=tot_length) channel4 = BitArray(int=0, length=tot_length) for x in range(len(ch1)): if (x%2 == 0): channel1.invert(range(ch1[x],ch1[x+1])) else: pass for x in range(len(ch2)): if (x%2 == 0): channel2.invert(range(ch2[x],ch2[x+1])) else: pass for x in range(len(ch3)): if (x%2 == 0): channel3.invert(range(ch3[x],ch3[x+1])) else: pass for x in range(len(ch4)): if (x%2 == 0): channel4.invert(range(ch4[x],ch4[x+1])) else: pass channel1 |= channel2 channel3 |= channel4 channel1 |= channel3 channel1.append('0b0') result = [] count = 0 test = BitArray(int = 0, length = 1) empty = test.find('0b1') while (channel1.len != 0): test = channel1.find('0b1') if (test == empty): break else: result.append(channel1.find('0b1')) result[count] = result[count][0] del channel1[0:result[count]] count = count + 1 test = channel1.find('0b0') if (test == empty): break else: result.append(channel1.find('0b0')) result[count] = result[count][0] del channel1[0:result[count]] count = count + 1 return result
class LifObject(base.ConfigObjectBase): def __init__(self): super().__init__() self.Clone(Store.templates.Get('LIF')) return def Init(self, tenant, spec, namespace=None): if namespace: self.id = namespace.get() else: self.id = resmgr.LifIdAllocator.get() self.GID("Lif%d" % self.id) self.status = haldefs.interface.IF_STATUS_UP self.hw_lif_id = -1 self.qstate_base = {} self.promiscuous = False self.allmulticast = False self.pds = [] self.c_lib_name = getattr(spec, 'c_lib', None) if self.c_lib_name: self.c_lib = clibs.LoadCLib(self.c_lib_name) if self.c_lib: self.c_lib_config = self.c_lib[self.c_lib_name + '_config'] self.c_lib_config.argtypes = [ctypes.POINTER(QInfoStruct)] self.c_lib_config.restype = None else: self.c_lib = None if hasattr(spec, 'rdma') and spec.rdma.enable: self.enable_rdma = spec.rdma.enable self.rdma_max_pt_entries = spec.rdma.max_pt_entries self.rdma_max_keys = spec.rdma.max_keys self.rdma_max_ahs = spec.rdma.max_ahs self.hostmem_pg_size = spec.rdma.hostmem_pg_size self.hbm_barmap_entries = (int(spec.rdma.hbm_barmap_size / spec.rdma.hostmem_pg_size)) self.rdma_tbl_pos = BitArray(length=self.rdma_max_pt_entries) self.hbm_tbl_pos = BitArray(length=self.hbm_barmap_entries) self.rdma_async_eq_id = 0 self.rdma_admin_aq_id = 1 self.rdma_admin_cq_id = 0 else: self.enable_rdma = False self.rdma_max_pt_entries = 0 self.rdma_max_keys = 0 self.rdma_max_ahs = 0 self.hbm_barmap_entries = 0 self.hostmem_pg_size = 0 self.rdma_pt_base_addr = 0 self.rdma_kt_base_addr = 0 self.rdma_dcqcn_profile_base_addr = 0 self.rdma_at_base_addr = 0 if hasattr(spec, 'nvme') and spec.nvme.enable: self.enable_nvme = spec.nvme.enable self.nvme_lif = nvme_lif.NvmeLifObject(self, spec.nvme) else: self.enable_nvme = False self.vlan_strip_en = False self.vlan_insert_en = False self.queue_types = objects.ObjectDatabase() self.obj_helper_q = queue_type.QueueTypeObjectHelper() self.obj_helper_q.Generate(self, spec) self.queue_types.SetAll(self.obj_helper_q.queue_types) self.queue_types_list = self.obj_helper_q.queue_types self.queue_list = [] for q_type in self.queue_types_list: for queue in q_type.queues.GetAll(): self.queue_list.append(queue) # RDMA per LIF allocators if self.enable_rdma: # QP 0, 1 are special QPs self.qpid_allocator = objects.TemplateFieldObject( "range/2/" + str(spec.rdma.max_qp)) # AQ ID 0 is owned by ETH self.aqid_allocator = objects.TemplateFieldObject( "range/1/" + str(spec.rdma.max_aq)) # Reserve CQ 0, 1 for special QPs, AQ self.cqid_allocator = objects.TemplateFieldObject( "range/2/" + str(spec.rdma.max_cq)) self.eqid_allocator = objects.TemplateFieldObject( "range/0/" + str(spec.rdma.max_eq)) self.pd_allocator = objects.TemplateFieldObject( "range/0/" + str(spec.rdma.max_pd)) self.mr_key_allocator = objects.TemplateFieldObject( "range/1/" + str(spec.rdma.max_mr)) self.slab_allocator = objects.TemplateFieldObject("range/0/2048") self.kslab_allocator = objects.TemplateFieldObject("range/0/2048") # Generate RDMA LIF owned resources self.slabs = objects.ObjectDatabase() # Generate KernelSlab of 4KB and 10 MB for FRPMR self.kslab_4KB = slab.SlabObject(self, 4096, True) self.obj_helper_slab = slab.SlabObjectHelper() #slab_spec = spec.rdma.slab.Get(Store) #self.obj_helper_slab.Generate(self, slab_spec) #self.slabs.SetAll(self.obj_helper_slab.slabs) # Create EQs for RDMA LIF self.eqs = objects.ObjectDatabase() self.obj_helper_eq = eq.EqObjectHelper() self.obj_helper_eq.Generate(self, spec.rdma.max_eq, spec.rdma.max_eqe) if len(self.obj_helper_eq.eqs): self.eqs.SetAll(self.obj_helper_eq.eqs) # Create CQ 0 for adminQ logger.info("Creating 1 Cqs. for LIF:%s" % (self.GID())) # Hardcode CQ 0 for AQ # Page size is calculated as max_cqe * cqe_size by the CQ for privileged resources cq_id = 0 self.cq = cqs.CqObject(None, cq_id, spec.rdma.max_cqe, 0, True, self) # Create AdminQ logger.info("Creating 1 Aqs. for LIF:%s" % (self.GID())) aq_id = self.GetAqid() self.aq = aqs.AqObject(self, aq_id, spec.rdma.max_aqe, spec.rdma.hostmem_pg_size) self.dcqcn_config_spec = spec.rdma.dcqcn_config.Get(Store) if hasattr(spec, 'rss'): self.rss_type = ( haldefs.interface.LifRssType.Value("RSS_TYPE_IPV4") | haldefs.interface.LifRssType.Value("RSS_TYPE_IPV4_TCP") | haldefs.interface.LifRssType.Value("RSS_TYPE_IPV4_UDP") | haldefs.interface.LifRssType.Value("RSS_TYPE_IPV6") | haldefs.interface.LifRssType.Value("RSS_TYPE_IPV6_TCP") | haldefs.interface.LifRssType.Value("RSS_TYPE_IPV6_UDP")) self.rss_key = array.array('B', toeplitz.toeplitz_msft_key) self.rss_indir = array.array('B', [0] * 128) else: self.rss_type = haldefs.interface.LifRssType.Value("RSS_TYPE_NONE") self.rss_key = array.array('B', toeplitz.toeplitz_msft_key) self.rss_indir = array.array('B', [0] * 128) self.tenant = tenant self.spec = spec self.tx_qos_class = None self.rx_qos_class = None if self.tenant.IsQosEnabled(): self.tx_qos_class = getattr(spec, 'tx_qos_class', None) self.rx_qos_class = getattr(spec, 'rx_qos_class', None) if self.tx_qos_class: self.tx_qos_class = Store.objects.Get(self.tx_qos_class) if self.rx_qos_class: self.rx_qos_class = Store.objects.Get(self.rx_qos_class) self.Show() def GetQpid(self): return self.qpid_allocator.get() def GetCqid(self): return self.cqid_allocator.get() def GetEqid(self): return self.eqid_allocator.get() def GetAqid(self): return self.aqid_allocator.get() def GetSlabid(self): return self.slab_allocator.get() def GetKSlabid(self): return self.kslab_allocator.get() def GetPd(self): return self.pd_allocator.get() def GetMrKey(self): return self.mr_key_allocator.get() def GetRdmaTblPos(self, num_pages): if num_pages <= 0: return total_pages = int(num_pages) if total_pages <= 8: total_pages = 8 else: total_pages = 1 << (total_pages - 1).bit_length() logger.info( "- LIF: %s Requested: %d Actual: %d page allocation in RDMA PT Table" % (self.GID(), num_pages, total_pages)) page_order = BitArray(length=total_pages) page_order.set(False) tbl_pos = self.rdma_tbl_pos.find(page_order) assert (tbl_pos) self.rdma_tbl_pos.set(True, range(tbl_pos[0], tbl_pos[0] + total_pages)) return tbl_pos[0] def GetHbmTblPos(self, num_pages): if num_pages <= 0: return total_pages = int(num_pages) if total_pages <= 8: total_pages = 8 else: total_pages = 1 << (total_pages - 1).bit_length() logger.info( "- LIF: %s Requested: %d Actual: %d page allocation in NIC HBM barmap area" % (self.GID(), num_pages, total_pages)) page_order = BitArray(length=total_pages) page_order.set(False) tbl_pos = self.hbm_tbl_pos.find(page_order) assert (tbl_pos) self.hbm_tbl_pos.set(True, range(tbl_pos[0], tbl_pos[0] + total_pages)) return tbl_pos[0] def GetQt(self, type): return self.queue_types.Get(type) def GetQ(self, type, qid): qt = self.queue_types.Get(type) if qt is not None: return qt.queues.Get(str(qid)) def GetQstateAddr(self, type): if GlobalOptions.dryrun: return 0 return self.qstate_base[type] def GetTxQosCos(self): if self.tx_qos_class: return self.tx_qos_class.GetTxQosCos() return 7 def GetTxQosDscp(self): if self.tx_qos_class: return self.tx_qos_class.GetTxQosDscp() return 7 def ConfigureQueueTypes(self): if GlobalOptions.dryrun: return 0 self.obj_helper_q.Configure() def ConfigureRdmaLifRes(self): if self.enable_rdma: # EQID 0 on LIF is used for Async events/errors across all PDs self.async_eq = self.GetQ('RDMA_EQ', self.rdma_async_eq_id) fail = self.async_eq is None self.admin_eq = self.async_eq # Get EQID 0, CQID 0 for Admin queue AQ 0 self.admin_cq = self.GetQ('RDMA_CQ', self.rdma_admin_cq_id) fail = fail or self.admin_cq is None # AQ position in list is different from AQ qid self.adminq = self.GetQ('RDMA_AQ', self.rdma_admin_aq_id) fail = fail or self.adminq is None if (fail is True): assert (0) self.obj_helper_slab.Configure() self.kslab_4KB.Configure() if len(self.obj_helper_eq.eqs): self.obj_helper_eq.Configure() halapi.ConfigureCqs([self.cq]) halapi.ConfigureAqs([self.aq]) logger.info("Configuring DCQCN Configs for LIF:%s" % (self.GID())) self.dcqcn_config_helper = dcqcn.RdmaDcqcnProfileObjectHelper() self.dcqcn_config_helper.Generate(self, self.dcqcn_config_spec) self.dcqcn_config_helper.Configure() return 0 def Show(self): logger.info("- LIF : %s" % self.GID()) logger.info(" - # Queue Types : %d" % len(self.obj_helper_q.queue_types)) def PrepareHALRequestSpec(self, req_spec): req_spec.key_or_handle.lif_id = self.id req_spec.admin_status = self.status req_spec.enable_rdma = self.enable_rdma req_spec.rdma_max_keys = self.rdma_max_keys req_spec.rdma_max_ahs = self.rdma_max_ahs req_spec.rdma_max_pt_entries = self.rdma_max_pt_entries req_spec.vlan_strip_en = self.vlan_strip_en req_spec.vlan_insert_en = self.vlan_insert_en req_spec.pinned_uplink_if_key_handle.interface_id = 0x41010001 if self.tx_qos_class: req_spec.tx_qos_class.qos_group = self.tx_qos_class.GroupEnum() if self.rx_qos_class: req_spec.rx_qos_class.qos_group = self.rx_qos_class.GroupEnum() if GlobalOptions.classic: req_spec.packet_filter.receive_broadcast = True req_spec.packet_filter.receive_promiscuous = self.promiscuous req_spec.packet_filter.receive_all_multicast = self.allmulticast req_spec.rss.type = self.rss_type req_spec.rss.key = bytes(self.rss_key) req_spec.rss.indir = bytes(self.rss_indir) req_spec.enable_nvme = self.enable_nvme if self.enable_nvme: req_spec.nvme_max_ns = self.spec.nvme.max_ns req_spec.nvme_max_sess = self.spec.nvme.max_sess req_spec.nvme_host_page_size = self.spec.nvme.host_page_size else: req_spec.nvme_max_ns = 0 req_spec.nvme_max_sess = 0 req_spec.nvme_host_page_size = 0 for queue_type in self.queue_types.GetAll(): qstate_map_spec = req_spec.lif_qstate_map.add() queue_type.PrepareHALRequestSpec(qstate_map_spec) for queue in queue_type.queues.GetAll(): qstate_spec = req_spec.lif_qstate.add() queue.PrepareHALRequestSpec(qstate_spec) def ProcessHALResponse(self, req_spec, resp_spec): self.hal_handle = resp_spec.status.lif_handle if (self.c_lib): self.CLibConfig(resp_spec) if self.hw_lif_id == -1: # HAL does not return hw_lif_id in the UpdateResponse. Set the hw_lif_id only once. self.hw_lif_id = resp_spec.status.hw_lif_id logger.info( "- LIF %s = %s HW_LIF_ID = %s (HDL = 0x%x)" % (self.GID(), haldefs.common.ApiStatus.Name( resp_spec.api_status), self.hw_lif_id, self.hal_handle)) for qstate in resp_spec.qstate: logger.info("- QUEUE_TYPE = %d QSTATE_ADDR = 0x%x" % (qstate.type_num, qstate.addr)) self.qstate_base[qstate.type_num] = qstate.addr if self.enable_rdma: if resp_spec.rdma_data_valid: self.rdma_pt_base_addr = resp_spec.rdma_data.pt_base_addr self.rdma_kt_base_addr = resp_spec.rdma_data.kt_base_addr self.rdma_dcqcn_profile_base_addr = resp_spec.rdma_data.dcqcn_profile_base_addr self.rdma_at_base_addr = resp_spec.rdma_data.at_base_addr self.hbm_barmap_base = resp_spec.rdma_data.barmap_base_addr logger.info( "- RDMA-DATA: LIF %s = HW_LIF_ID = %s %s= 0x%x %s= 0x%x %s= 0x%x %s= 0x%x %s= 0x%x" % (self.GID(), self.hw_lif_id, 'PT-Base-Addr', self.rdma_pt_base_addr, 'KT-Base-Addr', self.rdma_kt_base_addr, 'DCQCN-Prof-Base-Addr', self.rdma_dcqcn_profile_base_addr, 'AT-Base-Addr', self.rdma_at_base_addr, 'BARMAP-Base-Addr', self.hbm_barmap_base)) def PrepareHALGetRequestSpec(self, req_spec): req_spec.key_or_handle.lif_id = self.id return def ProcessHALGetResponse(self, req_spec, resp_spec): logger.info( "- GET LIF %s = %s" % (self.GID(), haldefs.common.ApiStatus.Name(resp_spec.api_status))) self.get_resp = copy.deepcopy(resp_spec) return def Get(self): halapi.GetLifs([self]) return def GetStats(self): if GlobalOptions.dryrun: return None self.Get() return self.get_resp.stats def IsFilterMatch(self, spec): return super().IsFilterMatch(spec.filters) def CLibConfig(self, resp_spec): qaddrs_type = ctypes.c_uint64 * 8 qaddrs = qaddrs_type() for qstate in resp_spec.qstate: qaddrs[int(qstate.type_num)] = qstate.addr qinfo = QInfoStruct(GlobalOptions.dryrun, resp_spec.status.hw_lif_id, qaddrs) self.c_lib_config(ctypes.byref(qinfo)) def Update(self): halapi.ConfigureLifs([self], update=True) def SetPromiscous(self): logger.info("Setting PROMISCUOUS mode for LIF:%s" % self.GID()) self.promiscuous = True return def IsPromiscous(self): return self.promiscuous def SetAllMulticast(self): logger.info("Setting ALL MULTICAST mode for LIF:%s" % self.GID()) self.allmulticast = True return def IsAllMulticast(self): return self.allmulticast def IsFilterMatch(self, spec): return super().IsFilterMatch(spec.filters) def RegisterPd(self, pd): if self.enable_rdma: logger.info("Registering PD: %s LIF %s" % (pd.GID(), self.GID())) self.pds.append(pd) def AddSlab(self, slab): self.obj_helper_slab.AddSlab(slab) self.slabs.Add(slab)
class VirtualMachineInterfaceKM(DBBaseKM): _dict = {} obj_type = 'virtual_machine_interface' _ann_fq_name_to_uuid = {} ann_fq_name_key = ["kind","name"] _fq_name_to_uuid = {} def __init__(self, uuid, obj_dict=None): self.uuid = uuid self.host_id = None self.virtual_network = None self.virtual_machine = None self.instance_ips = set() self.floating_ips = set() self.virtual_machine_interfaces = set() self.vlan_id = None self.vlan_bit_map = None self.security_groups = set() super(VirtualMachineInterfaceKM, self).__init__(uuid, obj_dict) obj_dict = self.update(obj_dict) self.add_to_parent(obj_dict) def update(self, obj=None): if obj is None: obj = self.read_obj(self.uuid) self.name = obj['fq_name'][-1] self.fq_name = obj['fq_name'] self.annotations = obj.get('annotations', None) self.build_fq_name_to_uuid(self.uuid, obj) # Cache bindings on this VMI. if obj.get('virtual_machine_interface_bindings', None): bindings = obj['virtual_machine_interface_bindings'] kvps = bindings.get('key_value_pair', None) for kvp in kvps or []: if kvp['key'] == 'host_id': self.host_id = kvp['value'] self.update_multiple_refs('instance_ip', obj) self.update_multiple_refs('floating_ip', obj) self.update_single_ref('virtual_network', obj) self.update_single_ref('virtual_machine', obj) self.update_multiple_refs('security_group', obj) self.update_multiple_refs('virtual_machine_interface', obj) # Update VMI properties. vlan_id = None if obj.get('virtual_machine_interface_properties', None): props = obj['virtual_machine_interface_properties'] # Property: Vlan ID. vlan_id = props.get('sub_interface_vlan_tag', None) # If vlan is configured on this interface, cache the appropriate # info. # # In nested mode, if the interface is a sub-interface, the vlan id # space is allocated and managed in the parent interface. So check to # see if the interface has a parent. If it does, invoke the appropriate # method on the parent interface for vlan-id management. if (vlan_id is not None or self.vlan_id is not None) and\ vlan_id is not self.vlan_id: # Vlan is configured on this interface. if DBBaseKM.is_nested(): # We are in nested mode. # Check if this interface has a parent. If yes, this is # is a sub-interface and the vlan-id should be managed on the # vlan-id space of the parent interface. parent_vmis = self.virtual_machine_interfaces for parent_vmi_id in parent_vmis: parent_vmi = VirtualMachineInterfaceKM.locate( parent_vmi_id) if not parent_vmi: continue if self.vlan_id is not None: parent_vmi.reset_vlan(self.vlan_id) if vlan_id is not None: parent_vmi.set_vlan(vlan_id) # Cache the Vlan-id. self.vlan_id = vlan_id else: # Nested mode is not configured. # Vlan-id is NOT managed on the parent interface. # Just cache and proceed. self.vlan_id = vlan_id return obj @classmethod def delete(cls, uuid): if uuid not in cls._dict: return obj = cls._dict[uuid] # If vlan-id is configured and if we are in nested mode, # free up the vlan-id which was claimed from the parent # interface. if obj.vlan_id and DBBaseKM.is_nested(): parent_vmi_ids = obj.virtual_machine_interfaces for parent_vmi_id in parent_vmi_ids: parent_vmi = VirtualMachineInterfaceKM.get(parent_vmi_id) if parent_vmi: parent_vmi.reset_vlan(obj.vlan_id) obj.update_multiple_refs('instance_ip', {}) obj.update_multiple_refs('floating_ip', {}) obj.update_single_ref('virtual_network', {}) obj.update_single_ref('virtual_machine', {}) obj.update_multiple_refs('security_group', {}) obj.update_multiple_refs('virtual_machine_interface', {}) obj.remove_from_parent() del cls._dict[uuid] def set_vlan (self, vlan): if vlan < MIN_VLAN_ID or vlan > MAX_VLAN_ID: return if not self.vlan_bit_map: self.vlan_bit_map = BitArray(MAX_VLAN_ID + 1) self.vlan_bit_map[vlan] = 1 def reset_vlan (self, vlan): if vlan < MIN_VLAN_ID or vlan > MAX_VLAN_ID: return if not self.vlan_bit_map: return self.vlan_bit_map[vlan] = 0 def alloc_vlan (self): if not self.vlan_bit_map: self.vlan_bit_map = BitArray(MAX_VLAN_ID + 1) vid = self.vlan_bit_map.find('0b0', MIN_VLAN_ID) if vid: self.set_vlan(vid[0]) return vid[0] return INVALID_VLAN_ID def get_vlan (self): return vlan_id
def parse(cls, address, data): '''Used to parse data received from RuuviTag. Currently supports versions 3 and 5 of the protocol. Arguments: address (str): MAC address of RuuviTag. data (bytes): received data in bytes. ''' b = BitArray(bytes=data) # Try to find protocol version 3 # https://github.com/ruuvi/ruuvi-sensor-protocols#data-format-3-protocol-specification if b.find('0xFF990403'): # If it's found, parse data b = list(b.split('0xFF990403', count=2))[-1] _, humidity, temperature_sign, temperature, temperature_fraction, pressure, \ accel_x, accel_y, accel_z, battery_voltage = b.unpack( # Ignore the packet type and manufacturer specs, # as they've been handled before 'uint:32,' + # Parse the actual payload 'uint:8, int:1, uint:7, uint:8, uint:16,' + 'int:16, int:16, int:16, uint:16') temperature_sign = -1 if temperature_sign == -1 else 1 # ... and return an instance of the calling class return cls(address, 3, temperature=temperature_sign * (float(temperature) + temperature_fraction / 100.0), humidity=humidity / 2.0, pressure=(pressure + 50000) / 100.0, acceleration_x=accel_x / 1000.0, acceleration_y=accel_y / 1000.0, acceleration_z=accel_z / 1000.0, battery_voltage=battery_voltage / 1000.0) # Try to find protocol version 5 # https://github.com/ruuvi/ruuvi-sensor-protocols#data-format-5-protocol-specification if b.find('0xFF990405'): # If it's found, parse data b = list(b.split('0xFF990405', count=2))[-1] _, temperature, humidity, pressure, \ accel_x, accel_y, accel_z, \ battery_voltage, tx_power, \ movement_counter, measurement_sequence, mac = b.unpack( # Ignore the packet type and manufacturer specs, # as they've been handled before 'uint:32,' + # Parse the actual payload 'int:16, uint:16, uint:16,' + 'int:16, int:16, int:16,' + 'uint:11, uint:5,' + 'uint:8, uint:16, uint:48') # Not sure what to do with MAC at the moment? # Maybe compare it to the one received by btle and # raise an exception if doesn't match? mac = '%x' % mac mac = ':'.join(mac[i:i + 2] for i in range(0, 12, 2)) # ... and return an instance of the calling class return cls(address, 5, temperature=float(temperature) * 0.005, humidity=humidity * 0.0025, pressure=(pressure + 50000) / 100.0, acceleration_x=accel_x / 1000.0, acceleration_y=accel_y / 1000.0, acceleration_z=accel_z / 1000.0, battery_voltage=(battery_voltage + 1600) / 1000.0, tx_power=-40 + tx_power, movement_counter=movement_counter, measurement_sequence=measurement_sequence)