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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>WiSet module — pyroute2 0.5.14 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/graphviz.css" />
<link rel="stylesheet" type="text/css" href="_static/custom.css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="IPSet module" href="ipset.html" />
<link rel="prev" title="IPDB guide" href="ipdb.html" />
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="ipset.html" title="IPSet module"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="ipdb.html" title="IPDB guide"
accesskey="P">previous</a> |</li>
<li class="nav-item"><a href="http://pyroute2.org">Project home</a> »</li>
<li class="nav-item nav-item-0"><a href="index.html">pyroute2 0.5.14 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-pyroute2.wiset">
<span id="wiset-module"></span><h1>WiSet module<a class="headerlink" href="#module-pyroute2.wiset" title="Permalink to this headline">¶</a></h1>
<p>High level ipset support.</p>
<p>When <a class="reference internal" href="ipset.html"><span class="doc">IPSet module</span></a> is providing a direct netlink socket with low level
functions, a <a class="reference internal" href="#pyroute2.wiset.WiSet" title="pyroute2.wiset.WiSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">WiSet</span></code></a> object is built to map ipset objects from kernel.
It helps to add/remove entries, list content, etc.</p>
<p>For example, adding an entry with <a class="reference internal" href="ipset.html#pyroute2.ipset.IPSet" title="pyroute2.ipset.IPSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">pyroute2.ipset.IPSet</span></code></a> object
implies to set a various number of parameters:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">ipset</span> <span class="o">=</span> <span class="n">IPSet</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">ipset</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"1.2.3.4/24"</span><span class="p">,</span> <span class="n">etype</span><span class="o">=</span><span class="s2">"net"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ipset</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>When they are discovered by a <a class="reference internal" href="#pyroute2.wiset.WiSet" title="pyroute2.wiset.WiSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">WiSet</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wiset</span> <span class="o">=</span> <span class="n">load_ipset</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">wiset</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s2">"1.2.3.4/24"</span><span class="p">)</span>
</pre></div>
</div>
<p>Listing entries is also easier using <a class="reference internal" href="#pyroute2.wiset.WiSet" title="pyroute2.wiset.WiSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">WiSet</span></code></a>, since it parses for you
netlink messages:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wiset</span><span class="o">.</span><span class="n">content</span>
<span class="go">{'1.2.3.0/24': IPStats(packets=None, bytes=None, comment=None,</span>
<span class="go"> timeout=None, skbmark=None, physdev=False)}</span>
</pre></div>
</div>
<dl class="function">
<dt id="pyroute2.wiset.need_ipset_socket">
<code class="sig-prename descclassname">pyroute2.wiset.</code><code class="sig-name descname">need_ipset_socket</code><span class="sig-paren">(</span><em class="sig-param">fun</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.need_ipset_socket" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorator to create netlink socket if needed.</p>
<p>In many of our helpers, we need to open a netlink socket. This can
be expensive for someone using many times the functions: instead to have
only one socket and use several requests, we will open it again and again.</p>
<p>This helper allow our functions to be flexible: the caller can pass an
optional socket, or do nothing. In this last case, this decorator
will open a socket for the caller (and close it after call)</p>
<p>It also help to mix helpers. One helper can call another one: the socket
will be opened only once. We just have to pass the ipset variable.</p>
<p>Note that all functions using this helper <em>must</em> use ipset as variable
name for the socket.</p>
</dd></dl>
<dl class="class">
<dt id="pyroute2.wiset.IPStats">
<em class="property">class </em><code class="sig-prename descclassname">pyroute2.wiset.</code><code class="sig-name descname">IPStats</code><a class="headerlink" href="#pyroute2.wiset.IPStats" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="class">
<dt id="pyroute2.wiset.WiSet">
<em class="property">class </em><code class="sig-prename descclassname">pyroute2.wiset.</code><code class="sig-name descname">WiSet</code><span class="sig-paren">(</span><em class="sig-param">name=None</em>, <em class="sig-param">attr_type='hash:ip'</em>, <em class="sig-param">family=<AddressFamily.AF_INET: 2></em>, <em class="sig-param">sock=None</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">counters=False</em>, <em class="sig-param">comment=False</em>, <em class="sig-param">hashsize=None</em>, <em class="sig-param">revision=None</em>, <em class="sig-param">skbinfo=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet" title="Permalink to this definition">¶</a></dt>
<dd><p>Main high level ipset manipulation class.</p>
<p>Every high level ipset operation should be possible with this class,
you probably don’t need other helpers of this module, except tools
to load data from kernel (<code class="xref py py-func docutils literal notranslate"><span class="pre">load_all_ipsets()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">load_ipset()</span></code>)</p>
<p>For example, you can create and an entry in a ipset just with:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">WiSet</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"mysuperipset"</span><span class="p">)</span> <span class="k">as</span> <span class="n">myset</span><span class="p">:</span>
<span class="gp">>>> </span> <span class="n">myset</span><span class="o">.</span><span class="n">create</span><span class="p">()</span> <span class="c1"># add the ipset in the kernel</span>
<span class="gp">>>> </span> <span class="n">myset</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s2">"198.51.100.1"</span><span class="p">)</span> <span class="c1"># add one IP to the set</span>
</pre></div>
</div>
<p>Netlink sockets are opened by __enter__ and __exit__ function, so you don’t
have to manage it manually if you use the “with” keyword.</p>
<p>If you want to manage it manually (for example for long operation in
a daemon), you can do the following:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">myset</span> <span class="o">=</span> <span class="n">WiSet</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"mysuperipset"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">myset</span><span class="o">.</span><span class="n">open_netlink</span><span class="p">()</span>
<span class="gp">>>> </span><span class="c1"># do stuff</span>
<span class="gp">>>> </span><span class="n">myset</span><span class="o">.</span><span class="n">close_netlink</span><span class="p">()</span>
</pre></div>
</div>
<p>You can also don’t initiate at all any netlink socket, this code will work:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">myset</span> <span class="o">=</span> <span class="n">WiSet</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"mysuperipset"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">myset</span><span class="o">.</span><span class="n">create</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">myset</span><span class="o">.</span><span class="n">destroy</span><span class="p">()</span>
</pre></div>
</div>
<p>But do it very carefully. In that case, a netlink socket will be opened
in background for any operation. No socket will be leaked, but that
can consume resources.</p>
<p>You can also instantiate WiSet objects with <code class="xref py py-func docutils literal notranslate"><span class="pre">load_all_ipsets()</span></code> and
<code class="xref py py-func docutils literal notranslate"><span class="pre">load_ipset()</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">all_sets_dict</span> <span class="o">=</span> <span class="n">load_all_ipsets</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">one_set</span> <span class="o">=</span> <span class="n">load_ipset</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"myset"</span><span class="p">)</span>
</pre></div>
</div>
<p>Have a look on content variable if you need list of entries in the Set.</p>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.open_netlink">
<code class="sig-name descname">open_netlink</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.open_netlink" title="Permalink to this definition">¶</a></dt>
<dd><p>Open manually a netlink socket. You can use “with WiSet()” instead</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.close_netlink">
<code class="sig-name descname">close_netlink</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.close_netlink" title="Permalink to this definition">¶</a></dt>
<dd><p>Clone any opened netlink socket</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.from_netlink">
<em class="property">classmethod </em><code class="sig-name descname">from_netlink</code><span class="sig-paren">(</span><em class="sig-param">ndmsg</em>, <em class="sig-param">content=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.from_netlink" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a ipset objects based on a parsed netlink message</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>ndmsg</strong> – the netlink message to parse</p></li>
<li><p><strong>content</strong> (<em>bool</em>) – should we fill (and parse) entries info (can be slow
on very large set)</p></li>
</ul>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.update_dict_content">
<code class="sig-name descname">update_dict_content</code><span class="sig-paren">(</span><em class="sig-param">ndmsg</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.update_dict_content" title="Permalink to this definition">¶</a></dt>
<dd><p>Update a dictionary statistics with values sent in netlink message</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>ndmsg</strong> (<em>netlink message</em>) – the netlink message</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.create">
<code class="sig-name descname">create</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.create" title="Permalink to this definition">¶</a></dt>
<dd><p>Insert this Set in the kernel</p>
<p>Many options are set with python object attributes (like comments,
counters, etc). For non-supported type, kwargs are provided. See
<a class="reference internal" href="ipset.html"><span class="doc">IPSet module</span></a> documentation for more information.</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.destroy">
<code class="sig-name descname">destroy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.destroy" title="Permalink to this definition">¶</a></dt>
<dd><p>Destroy this ipset in the kernel list.</p>
<p>It does not delete this python object (any content or other stored
values are keep in memory). This function will fail if the ipset is
still referenced (by example in iptables rules), you have been warned.</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.add">
<code class="sig-name descname">add</code><span class="sig-paren">(</span><em class="sig-param">entry</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Add an entry in this ipset.</p>
<p>If counters are enabled on the set, reset by default the value when
we add the element. Without this reset, kernel sometimes store old
values and can add very strange behavior on counters.</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.delete">
<code class="sig-name descname">delete</code><span class="sig-paren">(</span><em class="sig-param">entry</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete/remove an entry in this ipset</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.test">
<code class="sig-name descname">test</code><span class="sig-paren">(</span><em class="sig-param">entry</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.test" title="Permalink to this definition">¶</a></dt>
<dd><p>Test if an entry is in this ipset</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.test_list">
<code class="sig-name descname">test_list</code><span class="sig-paren">(</span><em class="sig-param">entries</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.test_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Test if a list of a set of entries is in this ipset</p>
<p>Return a set of entries found in the IPSet</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.update_content">
<code class="sig-name descname">update_content</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.update_content" title="Permalink to this definition">¶</a></dt>
<dd><p>Update the content dictionary with values from kernel</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.flush">
<code class="sig-name descname">flush</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>Flush entries of the ipset</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.content">
<em class="property">property </em><code class="sig-name descname">content</code><a class="headerlink" href="#pyroute2.wiset.WiSet.content" title="Permalink to this definition">¶</a></dt>
<dd><p>Dictionary of entries in the set.</p>
<p>Keys are IP addresses (as string), values are IPStats tuples.</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.insert_list">
<code class="sig-name descname">insert_list</code><span class="sig-paren">(</span><em class="sig-param">entries</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.insert_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Just a small helper to reduce the number of loops in main code.</p>
</dd></dl>
<dl class="method">
<dt id="pyroute2.wiset.WiSet.replace_entries">
<code class="sig-name descname">replace_entries</code><span class="sig-paren">(</span><em class="sig-param">new_list</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.WiSet.replace_entries" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace the content of an ipset with a new list of entries.</p>
<p>This operation is like a flush() and adding all entries one by one. But
this call is atomic: it creates a temporary ipset and swap the content.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>new_list</strong> (list or <code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code> of basestring or of) – list of entries to add</p>
</dd>
</dl>
<p>keyword arguments dict</p>
</dd></dl>
</dd></dl>
<dl class="function">
<dt id="pyroute2.wiset.get_ipset_socket">
<code class="sig-prename descclassname">pyroute2.wiset.</code><code class="sig-name descname">get_ipset_socket</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.wiset.get_ipset_socket" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a socket that one can pass to several WiSet objects</p>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="ipdb.html"
title="previous chapter">IPDB guide</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="ipset.html"
title="next chapter">IPSet module</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/wiset.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="ipset.html" title="IPSet module"
>next</a> |</li>
<li class="right" >
<a href="ipdb.html" title="IPDB guide"
>previous</a> |</li>
<li class="nav-item"><a href="http://pyroute2.org">Project home</a> »</li>
<li class="nav-item nav-item-0"><a href="index.html">pyroute2 0.5.14 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2013, Peter V. Saveliev.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 2.1.2.
</div>
</body>
</html>
|