???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
:)g              B   @   s4  d dl mZ yd dlmZ W n  ek
r<   d dlmZ Y nX d dlmZ d dlZ	d dl
mZ d dlZd dlZd dlZd dlZddlmZmZmZmZmZ ddlmZ dd	lmZ dZd
ZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/d Z0d!Z1d"Z2d#Z3d$Z4d%Z5ed&ed'ed(ed)ed*ed+ed,e d-e!d.e"d/e#d0e$d1e%d2e&d3e'd4e(d5e)d6e*d7e+d8e,d9e-d:e.d;e/d<e0d=e1d>e2d?e3d@e4dAe5dBiZ6dCa7dDZ8dEZ9dFdG Z:d~dHdIZ;dJdK Z<dLdM Z=dNdO Z>dPdQ Z?dRdS Z@G dTdU dUeZAG dVdW dWeAZBG dXdY dYeBZCG dZd[ d[eAZDG d\d] d]eDZEG d^d_ d_eDZFG d`da daZGG dbdc dceDZHG ddde deeDZIG dfdg dgeBZJG dhdi dieJZKG djdk dkeDZLG dldm dmeAZMG dndo doeAZNG dpdq dqeDZOG drds dseDZPG dtdu dueDZQG dvdw dweDZRG dxdy dyeBZSG dzd{ d{eAZTG d|d} d}ZUdS )    )print_function)MutableMapping)strftimeN)reduce   )lib_RAW_CB_TYPE_LOST_CB_TYPE_RINGBUF_CB_TYPEbcc_perf_buffer_opts)get_online_cpus)get_possible_cpus                     	   
                                                            ZHASHZARRAYZ
PROG_ARRAYZPERF_EVENT_ARRAYZPERCPU_HASHZPERCPU_ARRAYZSTACK_TRACEZCGROUP_ARRAYZLRU_HASHZLRU_PERCPU_HASHZLPM_TRIEZARRAY_OF_MAPSZHASH_OF_MAPSZDEVMAPZSOCKMAPZCPUMAPZXSKMAPZSOCKHASHZCGROUP_STORAGEZREUSEPORT_SOCKARRAYZPERCPU_CGROUP_STORAGEZQUEUEZSTACKZ
SK_STORAGEZDEVMAP_HASHZ
STRUCT_OPSZRINGBUFZINODE_STORAGEZTASK_STORAGE(   A   i  c             C   s\   d}d}x6|||  | d ks*||d kr,P |d7 }|d7 }q
W | |krX|d d d }|S )Nr    r   *+ )valval_maxwidthitextr0   r0   /usr/lib/python3.6/table.py_starsc   s     r7   c       
      C   s   g }d}x$t t| D ]}| | dkr|}qW d}d}xft t| D ]V}|dkrD||krD|d }i }||d< t|d |d< t| | |d< |j| |}qDW td||d}	|r|d |	|d < t|	 d S )	Nr   r   r   zinterval-startzinterval-endcountz%Y-%m-%d %H:%M:%S)Ztsval_typedata)rangelenintappendr   print)
valsr9   section_bucketZ	hist_listZmax_nonzero_idxr4   indexprevZlist_objZ	histogramr0   r0   r6   _print_json_histo   s(    
rD   c             C   s  d}d}d}x,t | D ] \}}|dkr*|}||kr|}qW |dkrPd}d}	t}
nd}d}	ttd	 }
|dkrxt||  xtd|d D ]~}d|> d? }d|> d }||kr|d8 }| | }|r|rt|	||||
t|||
f  d
}qt|	||||
t|||
f  qW d S )N@   r   r       z#     %-19s : count     distributionz%10d -> %-10d : %-8d |%-*s|z-               %-29s : count     distributionz%20d -> %-20d : %-8d |%-*s|r   Fr/   )	enumerate	stars_maxr=   r?   r;   r7   )r@   r9   strip_leading_zerolog2_dist_maxidx_maxr2   r4   vheaderbodystarsZlowZhighr1   r0   r0   r6   _print_log2_hist   s<      rP   c          
   C   s   d}d}d}x,t | D ] \}}|dkr*|}||kr|}qW d}d}	t}
|dkrZt||  xdtd|d D ]R}| | }|r|rt|	|||
t|||
f  d}qjt|	|||
t|||
f  qjW d S )NrE   r   r   z#     %-13s : count     distributionz        %-10d : %-8d |%-*s|Fr/   )rG   rH   r?   r;   r7   )r@   r9   rI   rJ   rK   r2   r4   rL   rM   rN   rO   r1   r0   r0   r6   _print_linear_hist   s,      

rQ   c             C   s"   yt |  S  tk
r   dS X d S )Nz	<unknown>)map_type_nameKeyError)ttyper0   r0   r6   get_table_type_name   s    rU   c       
      C   s  t jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt j	t j
t j
t jt jt j
d t jd t jd}tjd}g }tj| jj| j}d}x||k rrtj| jj| j|j }tjd|}|jd}|jd}	tjd|	rd}	|j|	}yD|r |j|||jd t|jd f n|j|||	 f W n2 tk
rf   td	|	 tjd
 tjd Y nX |d7 }qW tdt j fd|iS )Nr   )charZs8zunsigned charu8zu8 *zchar *ZshortZs16zunsigned shortu16r=   Zs32enumzunsigned intu32Zlongzunsigned longz	long longZs64zunsigned long longZu64Z__int128zunsigned __int128zvoid *z([^ ]+) ?\[([0-9]+)\]$r   z	(.*)#(.*)r   zenum .*rY   zGType: '%s' not recognized. Please define the data with ctypes manually.)filer,   _fields_)!ctZc_charZc_ubyteZc_char_pZc_shortZc_ushortc_intc_uintZc_longZc_ulongZ
c_longlongZc_ulonglongZc_void_precompiler   Zbpf_perf_event_fieldsbpfmodule_nameZbpf_perf_event_fielddecodematchgroupr>   r=   rS   r?   sysstderrexittype	Structure)
Z	event_mapZ
ct_mappingZ
array_typeZfields
num_fieldsr4   ZfieldmZ
field_nameZ
field_typer0   r0   r6   _get_event_class   s\    




(ro   c       	      K   s8  t j| j|}d}|tkr.t| ||||}n|tkrJt| ||||}n|tkrft| ||||}n|t	krt
| |||||}n|tkrt| ||||f|}nz|tkrt| ||||f|}nZ|tkrt| ||||}n>|tkrt| ||||}n"|tkrt| ||||}n|tkr6t| ||||}n|tkrRt| ||||}n|tkrnt| ||||}n|tkrt| ||||}n|tkrt| ||||}nx|tkrt | ||||}n\|t!krt"| ||||}n@|t#ks|t$krt%| |||}n|t&krt'| |||||}|dkr4t(d| |S )z|Table(bpf, map_id, map_fd, keytype, leaftype, **kwargs)

    Create a python object out of a reference to a bpf table handleNzUnknown table type %d))r   bpf_table_type_idrc   BPF_MAP_TYPE_HASH	HashTableBPF_MAP_TYPE_ARRAYArrayBPF_MAP_TYPE_PROG_ARRAY	ProgArrayBPF_MAP_TYPE_PERF_EVENT_ARRAYPerfEventArrayBPF_MAP_TYPE_PERCPU_HASH
PerCpuHashBPF_MAP_TYPE_PERCPU_ARRAYPerCpuArrayBPF_MAP_TYPE_LPM_TRIELpmTrieBPF_MAP_TYPE_STACK_TRACE
StackTraceBPF_MAP_TYPE_LRU_HASHLruHashBPF_MAP_TYPE_LRU_PERCPU_HASHLruPerCpuHashBPF_MAP_TYPE_CGROUP_ARRAYCgroupArrayBPF_MAP_TYPE_DEVMAPDevMapBPF_MAP_TYPE_CPUMAPCpuMapBPF_MAP_TYPE_XSKMAPXskMapBPF_MAP_TYPE_ARRAY_OF_MAPSMapInMapArrayBPF_MAP_TYPE_HASH_OF_MAPSMapInMapHashBPF_MAP_TYPE_QUEUEBPF_MAP_TYPE_STACK
QueueStackBPF_MAP_TYPE_RINGBUFRingBuf	Exception)	rb   map_idmap_fdkeytypeleaftypenamekwargsrT   tr0   r0   r6   Table  sR    









r   c               @   s  e Zd ZdDddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdEdd ZdFd!d"Zd#d$ ZdGd%d&Zd'd( Zd)d* ZdHd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZG d6d7 d7eZd8d9 Zd:d; ZdId>d?Z dJd@dAZ!dKdBdCZ"dS )L	TableBaseNc             C   sn   || _ || _|| _|| _|| _tj| j j| j| _tj	| j j| j| _
i | _|| _ttj| j j| j| _d S )N)rb   r   r   KeyLeafr   rp   rc   rT   bpf_table_flags_idflags_cbsrd   r=   bpf_table_max_entries_idmax_entries)selfrb   r   r   r   r   r   r0   r0   r6   __init__?  s    zTableBase.__init__c             C   s   | j S )N)r   )r   r0   r0   r6   get_fdL  s    zTableBase.get_fdc             C   sN   t jt j| jd }tj| jj| j|t	|t j
|}|dk rHtd|jS )Nr   r   zCould not printf key)r]   create_string_buffersizeofr   r   Zbpf_table_key_snprintfrb   rc   r   r<   byrefr   value)r   keybufresr0   r0   r6   key_sprintfO  s    zTableBase.key_sprintfc             C   sN   t jt j| jd }tj| jj| j|t	|t j
|}|dk rHtd|jS )Nr   r   zCould not printf leaf)r]   r   r   r   r   bpf_table_leaf_snprintfrb   rc   r   r<   r   r   r   )r   leafr   r   r0   r0   r6   leaf_sprintfW  s    zTableBase.leaf_sprintfc             C   s8   | j  }tj| jj| j|tj|}|dk r4td|S )Nr   zCould not scanf key)	r   r   Zbpf_table_key_sscanfrb   rc   r   r]   r   r   )r   Zkey_strr   r   r0   r0   r6   	key_scanf_  s    zTableBase.key_scanfc             C   s8   | j  }tj| jj| j|tj|}|dk r4td|S )Nr   zCould not scanf leaf)	r   r   bpf_table_leaf_sscanfrb   rc   r   r]   r   r   )r   leaf_strr   r   r0   r0   r6   
leaf_scanfg  s    zTableBase.leaf_scanfc             C   s4   | j  }tj| jtj|tj|}|dk r0t|S )Nr   )r   r   bpf_lookup_elemr   r]   r   rS   )r   r   r   r   r0   r0   r6   __getitem__o  s
    zTableBase.__getitem__c             C   sD   t j| jtj|tj|d}|dk r@tjtj }td| d S )Nr   zCould not update table: %s)	r   bpf_update_elemr   r]   r   osstrerror	get_errnor   )r   r   r   r   errstrr0   r0   r6   __setitem__v  s    zTableBase.__setitem__c             C   s$   t j| jtj|}|dk r td S )Nr   )r   Zbpf_delete_elemr   r]   r   rS   )r   r   r   r0   r0   r6   __delitem__|  s    zTableBase.__delitem__c             c   s6   x0| D ](}y| | V  W q t k
r,   Y qX qW d S )N)rS   )r   r   r0   r0   r6   
itervalues  s
    
zTableBase.itervaluesc             c   s:   x4| D ],}y|| | fV  W q t k
r0   Y qX qW d S )N)rS   )r   r   r0   r0   r6   	iteritems  s
    
zTableBase.iteritemsc             C   s   dd | j  D S )Nc             S   s   g | ]}|qS r0   r0   ).0itemr0   r0   r6   
<listcomp>  s    z#TableBase.items.<locals>.<listcomp>)r   )r   r0   r0   r6   items  s    zTableBase.itemsc             C   s   dd | j  D S )Nc             S   s   g | ]}|qS r0   r0   )r   r   r0   r0   r6   r     s    z$TableBase.values.<locals>.<listcomp>)r   )r   r0   r0   r6   values  s    zTableBase.valuesc             C   s    x| j  D ]}| j| q
W d S )N)keysr   )r   kr0   r0   r6   clear  s    zTableBase.clearFc             C   sz   d }}| r$| r$t jdddfS |s0| j}n|dk sB|| jkrJtd|rZ| j|  }|rj| j|  }t j|||fS )a  Allocate keys and/or values arrays. Useful for in items_*_batch.

        Args:
            alloc_k (bool): True to allocate keys array, False otherwise.
            Default is False.
            alloc_v (bool): True to allocate values array, False otherwise.
            Default is False.
            count (int): number of elements in the array(s) to allocate. If
            count is None then it allocates the maximum number of elements i.e
            self.max_entries.

        Returns:
            tuple: (count, keys, values). Where count is ct.c_uint32,
            and keys and values an instance of ct.Array
        Raises:
            ValueError: If count is less than 1 or greater than
            self.max_entries.
        Nr   r   zWrong count)r]   c_uint32r   
ValueErrorr   r   )r   alloc_kalloc_vr8   r   r   r0   r0   r6   _alloc_keys_values  s    zTableBase._alloc_keys_valuesc             C   sv   d}xF||gD ]:}|rt |tjs&tt|}|dk s@|| jkrtdqW |rl|rlt|t|krltdtj|S )a  Check if the given keys or values have the right type and size.

        Args:
            keys (ct.Array): keys array to check
            values (ct.Array): values array to check
        Returns:
            ct.c_uint32 : the size of the array(s)
        Raises:
            ValueError: If length of arrays is less than 1 or greater than
            self.max_entries, or when both arrays length are different.
            TypeError: If the keys and values are not an instance of ct.Array
        r   r   zArray's length is wrongz(keys array length != values array length)
isinstancer]   rt   	TypeErrorr<   r   r   r   )r   r   r   Zarr_lenelemr0   r0   r6   _sanity_check_keys_values  s    z#TableBase._sanity_check_keys_valuesc             c   s(   x"| j ddD ]\}}||fV  qW dS )a  Look up all the key-value pairs in the map.

        Args:
            None
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been looked up.
        Notes: lookup batch on a keys subset is not supported by the kernel.
        F)deleteN))_items_lookup_and_optionally_delete_batch)r   r   rL   r0   r0   r6   items_lookup_batch  s    
zTableBase.items_lookup_batchc             C   sh   |dk	rP| j |d}tj| jtj|tj|}|dkrdtdtjtj	  nx| j
 D ]}dS W dS )a  Delete the key-value pairs related to the keys given as parameters.
        Note that if no key are given, it is faster to call
        lib.bpf_lookup_and_delete_batch than create keys array and then call
        lib.bpf_delete_batch on these keys.

        Args:
            ct_keys (ct.Array): keys array to delete. If an array of keys is
            given then it deletes all the related keys-values.
            If keys is None (default) then it deletes all entries.
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been deleted.
        Raises:
            Exception: If bpf syscall return value indicates an error.
        N)r   r   z#BPF_MAP_DELETE_BATCH has failed: %s)r   r   Zbpf_delete_batchr   r]   r   r   r   r   r   items_lookup_and_delete_batch)r   ct_keysct_cntr   _r0   r0   r6   items_delete_batch  s    zTableBase.items_delete_batchc             C   sT   | j ||d}tj| jtj|tj|tj|}|dkrPtdtjtj	  dS )aq  Update all the key-value pairs in the map provided.
        The arrays must be the same length, between 1 and the maximum number
        of entries.

        Args:
            ct_keys (ct.Array): keys array to update
            ct_values (ct.Array): values array to update
        Raises:
            Exception: If bpf syscall return value indicates an error.
        )r   r   r   z#BPF_MAP_UPDATE_BATCH has failed: %sN)
r   r   Zbpf_update_batchr   r]   r   r   r   r   r   )r   r   	ct_valuesr   r   r0   r0   r6   items_update_batch  s    zTableBase.items_update_batchc             c   s(   x"| j ddD ]\}}||fV  qW dS )a?  Look up and delete all the key-value pairs in the map.

        Args:
            None
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been looked up and deleted.
        Notes: lookup and delete batch on a keys subset is not supported by
        the kernel.
        T)r   N)r   )r   r   rL   r0   r0   r6   r      s    z'TableBase.items_lookup_and_delete_batchTc          	   c   s4  |dkrt j}d}n
t j}d}| jddd\}}}tjd }}d}	x|j|	 |_|| j|	rftj|ndtj|tj|tj	| j
|	 tj|tj	| j|	 tj|}
tj }|	|j7 }	|
dkr|tjkrtd|tj|f |
dkrP |	|jkrP |jdkrFP qFW x&td|	D ]}|| || fV  qW dS )a  Look up and optionally delete all the key-value pairs in the map.

        Args:
            delete (bool) : look up and delete the key-value pairs when True,
            else just look up.
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been looked up and deleted.
        Raises:
            Exception: If bpf syscall return value indicates an error.
        Notes: lookup and delete batch on a keys subset is not supported by
        the kernel.
        TZBPF_MAP_LOOKUP_AND_DELETE_BATCHZBPF_MAP_LOOKUP_BATCH)r   r   r   Nz%s has failed: %s)r   Zbpf_lookup_and_delete_batchZbpf_lookup_batchr   r]   r   r   r   r   r   r   r   r   errnoENOENTr   r   r   r;   )r   r   Z	bpf_batchZbpf_cmdZct_buf_sizer   r   Zct_out_batchr   Ztotalr   Zerrcoder4   r0   r0   r6   r   /  s<    


z3TableBase._items_lookup_and_optionally_delete_batchc             C   s&   x t | j D ]}| j | |< qW d S )N)listr   r   )r   r   r0   r0   r6   zerof  s    zTableBase.zeroc             C   s
   t j| S )N)r   Iter)r   r0   r0   r6   __iter__n  s    zTableBase.__iter__c             C   s   | j  S )N)r   )r   r0   r0   r6   iterq  s    zTableBase.iterc             C   s   | j  S )N)r   )r   r0   r0   r6   r   r  s    zTableBase.keysc               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
zTableBase.Iterc             C   s   || _ d | _d S )N)tabler   )r   r   r0   r0   r6   r   u  s    zTableBase.Iter.__init__c             C   s   | S )Nr0   )r   r0   r0   r6   r   x  s    zTableBase.Iter.__iter__c             C   s   | j  S )N)next)r   r0   r0   r6   __next__z  s    zTableBase.Iter.__next__c             C   s   | j j| j| _| jS )N)r   r   r   )r   r0   r0   r6   r   |  s    zTableBase.Iter.nextN)__name__
__module____qualname__r   r   r   r   r0   r0   r0   r6   r   t  s   r   c             C   s^   | j  }|d kr0tj| jtj|tj| j }ntj| jtj|tj|}|dk rZt |S )Nr   )	r   r   Zbpf_get_first_keyr   r]   r   r   Zbpf_get_next_keyStopIteration)r   r   Znext_keyr   r0   r0   r6   r     s    zTableBase.nextc             C   s   | j jd d }| j jd d }|dkrHt| j jdkrH| j jd d }xX| j D ]L\}}t||}	|rp||	}	|j|	dgt  }
||	< t||}|j|
|< qRW t|j	 }|r||}x|D ]}	|j
|	 qW d S )Nr   r   Z__pad_1r   r   )r   r\   r<   r   getattrgetlog2_index_maxr   r   r   r>   )r   tmpbuckets	bucket_fnbucket_sort_fnf1f2r   rL   bucketr@   ZslotZbuckets_lstr0   r0   r6   decode_c_struct  s     


zTableBase.decode_c_structr   
Bucket ptrc             C   s   t | j tjrfi }g }| j|||| xr|D ]2}|| }	|rL|||f}
n||f}
t|	||
 q.W n6dgt }	x | j D ]\}}|j|	|j< qzW t|	| dS )a.  print_json_hist(val_type="value", section_header="Bucket ptr",
                                   section_print_fn=None, bucket_fn=None,
                                   bucket_sort_fn=None):

                Prints a table as a json histogram. The table must be stored as
                log2. The val_type argument is optional, and is a column header.
                If the histogram has a secondary key, the dictionary will be split by secondary key
                If section_print_fn is not None, it will be passed the bucket value
                to format into a string as it sees fit. If bucket_fn is not None,
                it will be used to produce a bucket value for the histogram keys.
                If bucket_sort_fn is not None, it will be used to sort the buckets
                before iterating them, and it is useful when there are multiple fields
                in the secondary key.
                The maximum index allowed is log2_index_max (65), which will
                accommodate any 64-bit integer in the histogram.
                r   N)	r   r   r]   rl   r   rD   r   r   r   )r   r9   section_headersection_print_fnr   r   r   r   r   r@   rA   r   rL   r0   r0   r6   print_json_hist  s    

zTableBase.print_json_histc             C   s   t | j tjrvi }g }| j|||| x|D ]B}	||	 }
|rTtd|||	f  ntd||	f  t|
|| q.W n8dgt }
x | j D ]\}}|j	|
|j	< qW t|
|| dS )a  print_log2_hist(val_type="value", section_header="Bucket ptr",
                           section_print_fn=None, bucket_fn=None,
                           strip_leading_zero=None, bucket_sort_fn=None):

        Prints a table as a log2 histogram. The table must be stored as
        log2. The val_type argument is optional, and is a column header.
        If the histogram has a secondary key, multiple tables will print
        and section_header can be used as a header description for each.
        If section_print_fn is not None, it will be passed the bucket value
        to format into a string as it sees fit. If bucket_fn is not None,
        it will be used to produce a bucket value for the histogram keys.
        If the value of strip_leading_zero is not False, prints a histogram
        that is omitted leading zeros from the beginning.
        If bucket_sort_fn is not None, it will be used to sort the buckets
        before iterating them, and it is useful when there are multiple fields
        in the secondary key.
        The maximum index allowed is log2_index_max (65), which will
        accommodate any 64-bit integer in the histogram.
        z
%s = %sz
%s = %rr   N)
r   r   r]   rl   r   r?   rP   r   r   r   )r   r9   r   r   r   rI   r   r   r   r   r@   r   rL   r0   r0   r6   print_log2_hist  s    

zTableBase.print_log2_histc             C   s   t | j tjrvi }g }| j|||| x|D ]B}	||	 }
|rTtd|||	f  ntd||	f  t|
|| q.W nddgt }
xL| j D ]@\}}y|j	|
|j	< W q t
k
r   t
d|j	tf Y qX qW t|
|| dS )a  print_linear_hist(val_type="value", section_header="Bucket ptr",
                           section_print_fn=None, bucket_fn=None,
                           strip_leading_zero=None, bucket_sort_fn=None)

        Prints a table as a linear histogram. This is intended to span integer
        ranges, eg, from 0 to 100. The val_type argument is optional, and is a
        column header.  If the histogram has a secondary key, multiple tables
        will print and section_header can be used as a header description for
        each.  If section_print_fn is not None, it will be passed the bucket
        value to format into a string as it sees fit. If bucket_fn is not None,
        it will be used to produce a bucket value for the histogram keys.
        If the value of strip_leading_zero is not False, prints a histogram
        that is omitted leading zeros from the beginning.
        If bucket_sort_fn is not None, it will be used to sort the buckets
        before iterating them, and it is useful when there are multiple fields
        in the secondary key.
        The maximum index allowed is linear_index_max (1025), which is hoped
        to be sufficient for integer ranges spanned.
        z
%s = %sz
%s = %rr   z#Index in print_linear_hist() of %d zexceeds max of %d.Nz5Index in print_linear_hist() of %d exceeds max of %d.)r   r   r]   rl   r   r?   rQ   linear_index_maxr   r   
IndexError)r   r9   r   r   r   rI   r   r   r   r   r@   r   rL   r0   r0   r6   print_linear_hist  s&    

zTableBase.print_linear_hist)N)FFN)NN)N)T)r   r   NNN)r   r   NNNN)r   r   NNNN)#r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   objectr   r   r   r   r   r   r0   r0   r0   r6   r   =  sH   
	
#


7 
#  
&  r   c                   s$   e Zd Z fddZdd Z  ZS )rr   c                s   t t| j|| d S )N)superrr   r   )r   argsr   )	__class__r0   r6   r   "  s    zHashTable.__init__c             C   s   d}x| D ]}|d7 }q
W |S )Nr   r   r0   )r   r4   r   r0   r0   r6   __len__%  s    
 zHashTable.__len__)r   r   r   r   r  __classcell__r0   r0   )r  r6   rr   !  s   rr   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r   +  s    zLruHash.__init__)r   r   r   r   r  r0   r0   )r  r6   r   *  s   r   c                   sp   e Zd Z fddZdd Zdd Z fddZ fd	d
Z fddZdd Z	dd Z
G dd deZ  ZS )	ArrayBasec                s   t t| j|| d S )N)r  r  r   )r   r  r   )r  r0   r6   r   /  s    zArrayBase.__init__c             C   sV   t |tr(|dk rt| | }| j|}t |tjs<td|jt| krRtd|S )Nr   z#Array index must be an integer typezArray index out of range)r   r=   r<   r   r]   Z_SimpleCDatar   r   )r   r   r0   r0   r6   _normalize_key2  s    

zArrayBase._normalize_keyc             C   s   | j S )N)r   )r   r0   r0   r6   r  =  s    zArrayBase.__len__c                s   | j |}tt| j|S )N)r  r  r  r   )r   r   )r  r0   r6   r   @  s    
zArrayBase.__getitem__c                s    | j |}tt| j|| d S )N)r  r  r  r   )r   r   r   )r  r0   r6   r   D  s    
zArrayBase.__setitem__c                s   | j |}tt| j| d S )N)r  r  r  r   )r   r   )r  r0   r6   r   H  s    
zArrayBase.__delitem__c             C   sD   | j |}| j }tj| jtj|tj|d}|dk r@tdd S )Nr   zCould not clear item)r  r   r   r   r   r]   r   r   )r   r   r   r   r0   r0   r6   	clearitemL  s
    
zArrayBase.clearitemc             C   s   t j| | jS )N)r  r   r   )r   r0   r0   r6   r   S  s    zArrayBase.__iter__c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
zArrayBase.Iterc             C   s   || _ || _d| _d S )Nr   r/   )r   r   r4   )r   r   r   r0   r0   r6   r   W  s    zArrayBase.Iter.__init__c             C   s   | S )Nr0   )r   r0   r0   r6   r   \  s    zArrayBase.Iter.__iter__c             C   s   | j  S )N)r   )r   r0   r0   r6   r   ^  s    zArrayBase.Iter.__next__c             C   s0   |  j d7  _ | j t| jkr$t | j| j S )Nr   )r4   r<   r   r   r   )r   r0   r0   r6   r   `  s    zArrayBase.Iter.nextN)r   r   r   r   r   r   r   r0   r0   r0   r6   r   V  s   r   )r   r   r   r   r  r  r   r   r   r  r   r   r   r  r0   r0   )r  r6   r  .  s   r  c                   s$   e Zd Z fddZdd Z  ZS )rt   c                s   t t| j|| d S )N)r  rt   r   )r   r  r   )r  r0   r6   r   g  s    zArray.__init__c             C   s   | j | d S )N)r  )r   r   r0   r0   r6   r   j  s    zArray.__delitem__)r   r   r   r   r   r  r0   r0   )r  r6   rt   f  s   rt   c                   s(   e Zd Z fddZ fddZ  ZS )rv   c                s   t t| j|| d S )N)r  rv   r   )r   r  r   )r  r0   r6   r   o  s    zProgArray.__init__c                sD   t |tr| j|}t || jjr.| j|j}tt| j|| d S )N)	r   r=   r   rb   ZFunctionfdr  rv   r   )r   r   r   )r  r0   r6   r   r  s
    

zProgArray.__setitem__)r   r   r   r   r   r  r0   r0   )r  r6   rv   n  s   rv   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )FileDescc             C   s"   |d ks|dk rt d|| _d S )Nr   zInvalid file descriptor)r   r	  )r   r	  r0   r0   r6   r   z  s    zFileDesc.__init__c             C   s*   | j d k	r&| j dkr&tj| j  d | _ d S )Nr   )r	  r   close)r   r0   r0   r6   clean_up  s    zFileDesc.clean_upc             C   s   | j   d S )N)r  )r   r0   r0   r6   __del__  s    zFileDesc.__del__c             O   s   | S )Nr0   )r   r  r   r0   r0   r6   	__enter__  s    zFileDesc.__enter__c             O   s   | j   d S )N)r  )r   r  r   r0   r0   r6   __exit__  s    zFileDesc.__exit__N)r   r   r   r   r  r  r  r  r0   r0   r0   r6   r
  y  s
   r
  c                   s(   e Zd Z fddZ fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zCgroupArray.__init__c                st   t |tr$tt| j|| j| nLt |trhttj	|tj
 }tt| j|| j|j W d Q R X ntdd S )Nz1Cgroup array key must be either FD or cgroup path)r   r=   r  r   r   r   strr
  r   openO_RDONLYr	  r   )r   r   r   f)r  r0   r6   r     s    

&zCgroupArray.__setitem__)r   r   r   r   r   r  r0   r0   )r  r6   r     s   r   c                   sZ   e Zd Z fddZdd Z fddZdd ZdddZdd Zdd Z	dd Z
  ZS )rx   c                s"   t t| j|| i | _d | _d S )N)r  rx   r   _open_key_fds_event_class)r   r  r   )r  r0   r6   r     s    zPerfEventArray.__init__c             C   s&   t | jj }x|D ]
}| |= qW d S )N)r   r  r   )r   r   r   r0   r0   r6   r    s    
zPerfEventArray.__del__c                sx   || j krd S tt| j| t| |f}|| jjkr\tj| jj|  | jj|= | j	|= ntj
| j |  | j |= d S )N)r  r  rx   r   idrb   perf_buffersr   Zperf_reader_freer   Zbpf_close_perf_event_fd)r   r   Zkey_id)r  r0   r6   r     s    


zPerfEventArray.__delitem__c             C   s*   | j dkrt| | _ tj|tj| j jS )a	  event(data)

        When perf buffers are opened to receive custom perf event,
        the underlying event data struct which is defined in C in
        the BPF program can be deduced via this function. This avoids
        redundant definitions in Python.
        N)r  ro   r]   castPOINTERcontents)r   r:   r0   r0   r6   event  s    

zPerfEventArray.eventr   Nr   c             C   s>   ||d @ dkrt dx t D ]}| j||||| q W dS )az  open_perf_buffers(callback)

        Opens a set of per-cpu ring buffer to receive custom perf event
        data from the bpf program. The callback will be invoked for each
        event submitted from the kernel, up to millions per second. Use
        page_cnt to change the size of the per-cpu ring buffer. The value
        must be a power of two and defaults to 8.
        r   r   z+Perf buffer page_cnt must be a power of twoN)r   r   _open_perf_buffer)r   callbackpage_cntlost_cbwakeup_eventsr4   r0   r0   r6   open_perf_buffer  s    
zPerfEventArray.open_perf_bufferc                s    fdd}fdd}t |}r.t|n
tjd t}	t }
d|
_|
_||
_tj	||	d |tj
|
}|svtdtj|}| j|| | j< || jjt| f< ||	f| j< d| j< d S )	Nc                sP   y || W n: t k
rJ } z|jtjkr6t  n|W Y d d }~X nX d S )N)IOErrorr   EPIPErj   )r   r:   sizee)r  cpur0   r6   raw_cb_  s    z1PerfEventArray._open_perf_buffer.<locals>.raw_cb_c                sL   y | W n: t k
rF } z|jtjkr2t  n|W Y d d }~X nX d S )N)r"  r   r#  rj   )r   Zlostr%  )r  r0   r6   lost_cb_  s    z2PerfEventArray._open_perf_buffer.<locals>.lost_cb_r   zCould not open perf bufferr/   r/   )r   r	   r]   r  r   pidr&  r   r   Zbpf_open_perf_buffer_optsr   r   Zperf_reader_fdr   r   rb   r  r  r   r  )r   r&  r  r  r  r   r'  r(  fnZlost_fnZoptsreaderr	  r0   )r  r&  r  r6   r    s     
z PerfEventArray._open_perf_bufferc             C   sB   t j||d|}|dk r td| j|| | j|< || j|< d S )Nr   r   zbpf_open_perf_event failedr/   )r   Zbpf_open_perf_eventr   r   r   r  )r   r&  typconfigr	  r0   r0   r6   _open_perf_event  s
    zPerfEventArray._open_perf_eventc             C   s"   xt  D ]}| j||| qW dS )zopen_perf_event(typ, config)

        Configures the table such that calls from the bpf program to
        table.perf_read(CUR_CPU_IDENTIFIER) will return the hardware
        counter denoted by event ev on the local cpu.
        N)r   r.  )r   r,  r-  r4   r0   r0   r6   open_perf_event  s    zPerfEventArray.open_perf_event)r   Nr   )r   r   r   r   r  r   r  r!  r  r.  r/  r  r0   r0   )r  r6   rx     s   
"rx   c                   sT   e Zd Z fddZ fddZdd Z fddZd	d
 Zdd Zdd Z	  Z
S )rz   c                s   |j dd | _tt| j|| | j| _tt | _	t
j| jd | _| jdkr`| j| j	 | _n@| jt
jkr|t
j| j	 | _n$| jt
jkrt
j| j	 | _ntdd S )Nreducerr   r   zLeaf must be aligned to 8 bytes)popr0  r  rz   r   r   sLeafr<   r   	total_cpur]   r   	alignmentr_   c_uint64r^   c_int64r   )r   r  r   )r  r0   r6   r     s    
zPerCpuHash.__init__c                sT   t t| j|}| jdkr |}n0| j| j  }x td| jD ]}|| ||< q<W |S )Nr   )r  rz   r   r4  r2  r3  r;   )r   r   resultretr4   )r  r0   r6   getvalue  s    
zPerCpuHash.getvaluec             C   s&   | j rt| j | j|S | j|S d S )N)r0  r   r9  )r   r   r0   r0   r6   r   $  s    zPerCpuHash.__getitem__c                s   t t| j|| d S )N)r  rz   r   )r   r   r   )r  r0   r6   r   *  s    zPerCpuHash.__setitem__c             C   s,   t | j tjrtd| jt| j|S )Nz6Leaf must be an integer type for default sum functions)r   r   r]   rl   r   r2  sumr9  )r   r   r0   r0   r6   r:  -  s    zPerCpuHash.sumc             C   s,   t | j tjrtd| jt| j|S )Nz6Leaf must be an integer type for default max functions)r   r   r]   rl   r   r2  maxr9  )r   r   r0   r0   r6   r;  2  s    zPerCpuHash.maxc             C   s   | j |}|j| j S )N)r:  r   r3  )r   r   r7  r0   r0   r6   average7  s    
zPerCpuHash.average)r   r   r   r   r9  r   r   r:  r;  r<  r  r0   r0   )r  r6   rz     s   
rz   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r   <  s    zLruPerCpuHash.__init__)r   r   r   r   r  r0   r0   )r  r6   r   ;  s   r   c                   s\   e Zd Z fddZ fddZdd Z fddZd	d
 Zdd Zdd Z	dd Z
  ZS )r|   c                s   |j dd | _tt| j|| | j| _tt | _	t
j| jd | _| jdkr`| j| j	 | _n@| jt
jkr|t
j| j	 | _n$| jt
jkrt
j| j	 | _ntdd S )Nr0  r   r   zLeaf must be aligned to 8 bytes)r1  r0  r  r|   r   r   r2  r<   r   r3  r]   r   r4  r_   r5  r^   r6  r   )r   r  r   )r  r0   r6   r   @  s    
zPerCpuArray.__init__c                sT   t t| j|}| jdkr |}n0| j| j  }x td| jD ]}|| ||< q<W |S )Nr   )r  r|   r   r4  r2  r3  r;   )r   r   r7  r8  r4   )r  r0   r6   r9  R  s    
zPerCpuArray.getvaluec             C   s&   | j rt| j | j|S | j|S d S )N)r0  r   r9  )r   r   r0   r0   r6   r   \  s    zPerCpuArray.__getitem__c                s   t t| j|| d S )N)r  r|   r   )r   r   r   )r  r0   r6   r   b  s    zPerCpuArray.__setitem__c             C   s   | j | d S )N)r  )r   r   r0   r0   r6   r   e  s    zPerCpuArray.__delitem__c             C   s,   t | j tjrtd| jt| j|S )Nz6Leaf must be an integer type for default sum functions)r   r   r]   rl   r   r2  r:  r9  )r   r   r0   r0   r6   r:  i  s    zPerCpuArray.sumc             C   s,   t | j tjrtd| jt| j|S )Nz6Leaf must be an integer type for default max functions)r   r   r]   rl   r   r2  r;  r9  )r   r   r0   r0   r6   r;  n  s    zPerCpuArray.maxc             C   s   | j |}|j| j S )N)r:  r   r3  )r   r   r7  r0   r0   r6   r<  s  s    
zPerCpuArray.average)r   r   r   r   r9  r   r   r   r:  r;  r<  r  r0   r0   )r  r6   r|   ?  s   
r|   c                   s$   e Zd Z fddZdd Z  ZS )r~   c                s   t t| j|| d S )N)r  r~   r   )r   r  r   )r  r0   r6   r   x  s    zLpmTrie.__init__c             C   s   t d S )N)NotImplementedError)r   r0   r0   r6   r  {  s    zLpmTrie.__len__)r   r   r   r   r  r  r0   r0   )r  r6   r~   w  s   r~   c                   sZ   e Zd ZdZdZdZdZdZ fddZG dd	 d	e	Z
dddZdd Zdd Z  ZS )r      r   r   r   r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zStackTrace.__init__c               @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )zStackTrace.StackWalkerNc             C   s   || _ d| _|| _|| _d S )Nr   r/   )stacknresolver   )r   r?  r   rA  r0   r0   r6   r     s    zStackTrace.StackWalker.__init__c             C   s   | S )Nr0   )r   r0   r0   r6   r     s    zStackTrace.StackWalker.__iter__c             C   s   | j  S )N)r   )r   r0   r0   r6   r     s    zStackTrace.StackWalker.__next__c             C   s   |  j d7  _ | j tjkr t | jtj@ rZ| jj| j  }|jtj	ksR|jtj
krht n| jj| j  }|dkrvt | jr| j|S |S )Nr   r   )r@  r   	MAX_DEPTHr   r   BPF_F_STACK_BUILD_IDr?  ZtraceZstatusBPF_STACK_BUILD_ID_IPBPF_STACK_BUILD_ID_EMPTYZiprA  )r   Zaddrr0   r0   r6   r     s    zStackTrace.StackWalker.next)N)r   r   r   r   r   r   r   r0   r0   r0   r6   StackWalker  s   
rF  Nc             C   s   t j| | j| | j|S )N)r   rF  r   r   )r   Zstack_idrA  r0   r0   r6   walk  s    zStackTrace.walkc             C   s   d}x| D ]}|d7 }q
W |S )Nr   r   r0   )r   r4   r   r0   r0   r6   r    s    
 zStackTrace.__len__c             C   s   d S )Nr0   )r   r0   r0   r6   r     s    zStackTrace.clearrF   )N)r   r   r   rB  rC  rE  ZBPF_STACK_BUILD_ID_VALIDrD  r   r   rF  rG  r  r   r  r0   r0   )r  r6   r     s   
r   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zDevMap.__init__)r   r   r   r   r  r0   r0   )r  r6   r     s   r   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zCpuMap.__init__)r   r   r   r   r  r0   r0   )r  r6   r     s   r   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zXskMap.__init__)r   r   r   r   r  r0   r0   )r  r6   r     s   r   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zMapInMapArray.__init__)r   r   r   r   r  r0   r0   )r  r6   r     s   r   c                   s   e Zd Z fddZ  ZS )r   c                s   t t| j|| d S )N)r  r   r   )r   r  r   )r  r0   r6   r     s    zMapInMapHash.__init__)r   r   r   r   r  r0   r0   )r  r6   r     s   r   c                   sF   e Zd Z fddZdd Zdd Zdd Zd	d
 ZdddZ  Z	S )r   c                s"   t t| j|| d | _d | _d S )N)r  r   r   Z_ringbufr  )r   r  r   )r  r0   r6   r     s    zRingBuf.__init__c             C   s   d S )Nr0   )r   r   r0   r0   r6   Z	__delitem  s    zRingBuf.__delitemc             C   s   d S )Nr0   )r   r0   r0   r6   r    s    zRingBuf.__del__c             C   s   dS )Nr   r0   )r   r0   r0   r6   r    s    zRingBuf.__len__c             C   s*   | j dkrt| | _ tj|tj| j jS )a  event(data)

        When ring buffers are opened to receive custom event,
        the underlying event data struct which is defined in C in
        the BPF program can be deduced via this function. This avoids
        redundant definitions in Python.
        N)r  ro   r]   r  r  r  )r   r:   r0   r0   r6   r    s    

zRingBuf.eventNc                s4    fdd}t |}| jj| j|| || jd< dS )zopen_ring_buffer(callback)

        Opens a ring buffer to receive custom event data from the bpf program.
        The callback will be invoked for each event submitted from the kernel,
        up to millions per second.
        c                sn   y. | ||}yt |}W n   d}Y nX W n: tk
rh } z|jtjkrTt  n|W Y d d }~X nX |S )Nr   )r=   r"  r   r#  rj   )ctxr:   r$  r8  r%  )r  r0   r6   ringbuf_cb_  s    z-RingBuf.open_ring_buffer.<locals>.ringbuf_cb_r   N)r
   rb   Z_open_ring_bufferr   r   )r   r  rH  rI  r*  r0   )r  r6   open_ring_buffer  s    zRingBuf.open_ring_buffer)N)
r   r   r   r   Z_RingBuf__delitemr  r  r  rJ  r  r0   r0   )r  r6   r     s   r   c               @   sR   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dd ZdS )r   r   c             C   s\   || _ || _|| _|| _tj| j j| j| _tj| j j| j| _	t
tj| j j| j| _d S )N)rb   r   r   r   r   rp   rc   rT   r   r   r=   r   r   )r   rb   r   r   r   r0   r0   r6   r     s    zQueueStack.__init__c             C   sN   t jt j| jd }tj| jj| j|t	|t j
|}|dk rHtd|jS )Nr   r   zCould not printf leaf)r]   r   r   r   r   r   rb   rc   r   r<   r   r   r   )r   r   r   r   r0   r0   r6   r     s    zQueueStack.leaf_sprintfc             C   s8   | j  }tj| jj| j|tj|}|dk r4td|S )Nr   zCould not scanf leaf)	r   r   r   rb   rc   r   r]   r   r   )r   r   r   r   r0   r0   r6   r     s    zQueueStack.leaf_scanfr   c             C   s>   t j| jd tj||}|dk r:tjtj }td| d S )Nr   zCould not push to table: %s)	r   r   r   r]   r   r   r   r   r   )r   r   r   r   r   r0   r0   r6   push  s    zQueueStack.pushc             C   s2   | j  }tj| jd tj|}|dk r.td|S )Nr   zCould not pop from table)r   r   Zbpf_lookup_and_deleter   r]   r   rS   )r   r   r   r0   r0   r6   r1  $  s
    zQueueStack.popc             C   s2   | j  }tj| jd tj|}|dk r.td|S )Nr   zCould not peek table)r   r   r   r   r]   r   rS   )r   r   r   r0   r0   r6   peek+  s
    zQueueStack.peekc             c   s@   | j }x4|r:y| j V  |d8 }W q tk
r6   d S X qW d S )Nr   )r   r1  rS   )r   Zcntr0   r0   r6   r   2  s    
zQueueStack.itervaluesc             C   s   dd | j  D S )Nc             S   s   g | ]}|qS r0   r0   )r   r   r0   r0   r6   r   =  s    z%QueueStack.values.<locals>.<listcomp>)r   )r   r0   r0   r6   r   <  s    zQueueStack.valuesN)r   )r   r   r   Z	BPF_EXISTr   r   r   rK  r1  rL  r   r   r0   r0   r0   r6   r      s   


r   )N)VZ
__future__r   collections.abcr   ImportErrorcollectionsZtimer   Zctypesr]   	functoolsr   r   r   r`   rh   Zlibbccr   r   r	   r
   r   Zutilsr   r   rq   rs   ru   rw   ry   r{   r   r   r   r   r}   r   r   r   ZBPF_MAP_TYPE_SOCKMAPr   r   ZBPF_MAP_TYPE_SOCKHASHZBPF_MAP_TYPE_CGROUP_STORAGEZ BPF_MAP_TYPE_REUSEPORT_SOCKARRAYZ"BPF_MAP_TYPE_PERCPU_CGROUP_STORAGEr   r   ZBPF_MAP_TYPE_SK_STORAGEZBPF_MAP_TYPE_DEVMAP_HASHZBPF_MAP_TYPE_STRUCT_OPSr   ZBPF_MAP_TYPE_INODE_STORAGEZBPF_MAP_TYPE_TASK_STORAGErR   rH   r   r   r7   rD   rP   rQ   rU   ro   r   r   rr   r   r  rt   rv   r
  r   rx   rz   r   r|   r~   r   r   r   r   r   r   r   r   r0   r0   r0   r6   <module>   s   
&;0   g	8l4849