File: netns.html

package info (click to toggle)
pyroute2 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,028 kB
  • sloc: python: 19,815; ansic: 81; makefile: 14
file content (388 lines) | stat: -rw-r--r-- 27,977 bytes parent folder | download
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Netns management overview &#8212; pyroute2 0.5.2 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" href="_static/custom.css" type="text/css" />
    <script type="text/javascript" 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>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="MPLS howto" href="mpls.html" />
    <link rel="prev" title="IPSet module" href="ipset.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="mpls.html" title="MPLS howto"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ipset.html" title="IPSet module"
             accesskey="P">previous</a> |</li>
        <li class="nav-item"><a href="http://pyroute2.org">Project home</a> &#187;</li>
        <li class="nav-item nav-item-0"><a href="index.html">pyroute2 0.5.2 documentation</a> &#187;</li> 
      </ul>
        </div>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <span class="target" id="module-pyroute2.netns"></span><div class="section" id="netns-management-overview">
<h1>Netns management overview<a class="headerlink" href="#netns-management-overview" title="Permalink to this headline">¶</a></h1>
<p>Pyroute2 provides basic namespaces management support.
Here’s a quick overview of typical netns tasks and
related pyroute2 tools.</p>
<div class="section" id="move-an-interface-to-a-namespace">
<h2>Move an interface to a namespace<a class="headerlink" href="#move-an-interface-to-a-namespace" title="Permalink to this headline">¶</a></h2>
<p>Though this task is managed not via <cite>netns</cite> module, it
should be mentioned here as well. To move an interface
to a netns, one should provide IFLA_NET_NS_FD nla in
a set link RTNL request. The nla is an open FD number,
that refers to already created netns. The pyroute2
library provides also a possibility to specify not a
FD number, but a netns name as a string. In that case
the library will try to lookup the corresponding netns
in the standard location.</p>
<p>Create veth and move the peer to a netns with IPRoute:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">IPRoute</span>
<span class="n">ipr</span> <span class="o">=</span> <span class="n">IPRoute</span><span class="p">()</span>
<span class="n">ipr</span><span class="o">.</span><span class="n">link</span><span class="p">(</span><span class="s1">&#39;add&#39;</span><span class="p">,</span> <span class="n">ifname</span><span class="o">=</span><span class="s1">&#39;v0p0&#39;</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s1">&#39;veth&#39;</span><span class="p">,</span> <span class="n">peer</span><span class="o">=</span><span class="s1">&#39;v0p1&#39;</span><span class="p">)</span>
<span class="n">idx</span> <span class="o">=</span> <span class="n">ipr</span><span class="o">.</span><span class="n">link_lookup</span><span class="p">(</span><span class="n">ifname</span><span class="o">=</span><span class="s1">&#39;v0p1&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">ipr</span><span class="o">.</span><span class="n">link</span><span class="p">(</span><span class="s1">&#39;set&#39;</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">idx</span><span class="p">,</span> <span class="n">net_ns_fd</span><span class="o">=</span><span class="s1">&#39;netns_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Create veth and move the peer to a netns with IPDB:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">IPDB</span>
<span class="n">ipdb</span> <span class="o">=</span> <span class="n">IPDB</span><span class="p">()</span>
<span class="n">ipdb</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">ifname</span><span class="o">=</span><span class="s1">&#39;v0p0&#39;</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s1">&#39;veth&#39;</span><span class="p">,</span> <span class="n">peer</span><span class="o">=</span><span class="s1">&#39;v0p1&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="k">with</span> <span class="n">ipdb</span><span class="o">.</span><span class="n">interfaces</span><span class="o">.</span><span class="n">v0p1</span> <span class="k">as</span> <span class="n">i</span><span class="p">:</span>
    <span class="n">i</span><span class="o">.</span><span class="n">net_ns_fd</span> <span class="o">=</span> <span class="s1">&#39;netns_name&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="manage-interfaces-within-a-netns">
<h2>Manage interfaces within a netns<a class="headerlink" href="#manage-interfaces-within-a-netns" title="Permalink to this headline">¶</a></h2>
<p>This task can be done with <cite>NetNS</cite> objects. A <cite>NetNS</cite> object
spawns a child and runs it within a netns, providing the same
API as <cite>IPRoute</cite> does:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">NetNS</span>
<span class="n">ns</span> <span class="o">=</span> <span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;netns_name&#39;</span><span class="p">)</span>
<span class="c1"># do some stuff within the netns</span>
<span class="n">ns</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>One can even start <cite>IPDB</cite> on the top of <cite>NetNS</cite>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">NetNS</span>
<span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">IPDB</span>
<span class="n">ipdb</span> <span class="o">=</span> <span class="n">IPDB</span><span class="p">(</span><span class="n">nl</span><span class="o">=</span><span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;netns_name&#39;</span><span class="p">))</span>
<span class="c1"># do some stuff within the netns</span>
<span class="n">ipdb</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="spawn-a-process-within-a-netns">
<h2>Spawn a process within a netns<a class="headerlink" href="#spawn-a-process-within-a-netns" title="Permalink to this headline">¶</a></h2>
<p>For that purpose one can use <cite>NSPopen</cite> API. It works just
as normal <cite>Popen</cite>, but starts a process within a netns.</p>
</div>
<div class="section" id="list-set-create-and-remove-netns">
<h2>List, set, create and remove netns<a class="headerlink" href="#list-set-create-and-remove-netns" title="Permalink to this headline">¶</a></h2>
<p>These functions are described below. To use them, import
<cite>netns</cite> module:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">netns</span>
<span class="n">netns</span><span class="o">.</span><span class="n">listnetns</span><span class="p">()</span>
</pre></div>
</div>
<p>Please be aware, that in order to run system calls the
library uses <cite>ctypes</cite> module. It can fail on platforms
where SELinux is enforced. If the Python interpreter,
loading this module, dumps the core, one can check the
SELinux state with <cite>getenforce</cite> command.</p>
<dl class="function">
<dt id="pyroute2.netns.create">
<code class="descclassname">pyroute2.netns.</code><code class="descname">create</code><span class="sig-paren">(</span><em>netns</em>, <em>libc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.create" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a network namespace.</p>
</dd></dl>

<dl class="function">
<dt id="pyroute2.netns.listnetns">
<code class="descclassname">pyroute2.netns.</code><code class="descname">listnetns</code><span class="sig-paren">(</span><em>nspath=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.listnetns" title="Permalink to this definition">¶</a></dt>
<dd><p>List available network namespaces.</p>
</dd></dl>

<dl class="function">
<dt id="pyroute2.netns.remove">
<code class="descclassname">pyroute2.netns.</code><code class="descname">remove</code><span class="sig-paren">(</span><em>netns</em>, <em>libc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove a network namespace.</p>
</dd></dl>

<dl class="function">
<dt id="pyroute2.netns.setns">
<code class="descclassname">pyroute2.netns.</code><code class="descname">setns</code><span class="sig-paren">(</span><em>netns</em>, <em>flags=64</em>, <em>libc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.setns" title="Permalink to this definition">¶</a></dt>
<dd><p>Set netns for the current process.</p>
<p>The flags semantics is the same as for the <cite>open(2)</cite>
call:</p>
<blockquote>
<div><ul class="simple">
<li>O_CREAT – create netns, if doesn’t exist</li>
<li>O_CREAT | O_EXCL – create only if doesn’t exist</li>
</ul>
</div></blockquote>
<p>Changed in 0.5.1: the routine closes the ns fd if it’s
not provided via arguments.</p>
</dd></dl>

</div>
</div>
<span class="target" id="module-pyroute2.netns.nslink"></span><div class="section" id="netns">
<h1>NetNS<a class="headerlink" href="#netns" title="Permalink to this headline">¶</a></h1>
<p>A NetNS object is IPRoute-like. It runs in the main network
namespace, but also creates a proxy process running in
the required netns. All the netlink requests are done via
that proxy process.</p>
<p>NetNS supports standard IPRoute API, so can be used instead
of IPRoute, e.g., in IPDB:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># start the main network settings database:</span>
<span class="n">ipdb_main</span> <span class="o">=</span> <span class="n">IPDB</span><span class="p">()</span>
<span class="c1"># start the same for a netns:</span>
<span class="n">ipdb_test</span> <span class="o">=</span> <span class="n">IPDB</span><span class="p">(</span><span class="n">nl</span><span class="o">=</span><span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">))</span>

<span class="c1"># create VETH</span>
<span class="n">ipdb_main</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">ifname</span><span class="o">=</span><span class="s1">&#39;v0p0&#39;</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s1">&#39;veth&#39;</span><span class="p">,</span> <span class="n">peer</span><span class="o">=</span><span class="s1">&#39;v0p1&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

<span class="c1"># move peer VETH into the netns</span>
<span class="k">with</span> <span class="n">ipdb_main</span><span class="o">.</span><span class="n">interfaces</span><span class="o">.</span><span class="n">v0p1</span> <span class="k">as</span> <span class="n">veth</span><span class="p">:</span>
    <span class="n">veth</span><span class="o">.</span><span class="n">net_ns_fd</span> <span class="o">=</span> <span class="s1">&#39;test&#39;</span>

<span class="c1"># please keep in mind, that netns move clears all the settings</span>
<span class="c1"># on a VETH interface pair, so one should run netns assignment</span>
<span class="c1"># as a separate operation only</span>

<span class="c1"># assign addresses</span>
<span class="c1"># please notice, that `v0p1` is already in the `test` netns,</span>
<span class="c1"># so should be accessed via `ipdb_test`</span>
<span class="k">with</span> <span class="n">ipdb_main</span><span class="o">.</span><span class="n">interfaces</span><span class="o">.</span><span class="n">v0p0</span> <span class="k">as</span> <span class="n">veth</span><span class="p">:</span>
    <span class="n">veth</span><span class="o">.</span><span class="n">add_ip</span><span class="p">(</span><span class="s1">&#39;172.16.200.1/24&#39;</span><span class="p">)</span>
    <span class="n">veth</span><span class="o">.</span><span class="n">up</span><span class="p">()</span>
<span class="k">with</span> <span class="n">ipdb_test</span><span class="o">.</span><span class="n">interfaces</span><span class="o">.</span><span class="n">v0p1</span> <span class="k">as</span> <span class="n">veth</span><span class="p">:</span>
    <span class="n">veth</span><span class="o">.</span><span class="n">add_ip</span><span class="p">(</span><span class="s1">&#39;172.16.200.2/24&#39;</span><span class="p">)</span>
    <span class="n">veth</span><span class="o">.</span><span class="n">up</span><span class="p">()</span>
</pre></div>
</div>
<p>Please review also the test code, under <cite>tests/test_netns.py</cite> for
more examples.</p>
<p>By default, NetNS creates requested netns, if it doesn’t exist,
or uses existing one. To control this behaviour, one can use flags
as for <cite>open(2)</cite> system call:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># create a new netns or fail, if it already exists</span>
<span class="n">netns</span> <span class="o">=</span> <span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">O_CREAT</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">O_EXCL</span><span class="p">)</span>

<span class="c1"># create a new netns or use existing one</span>
<span class="n">netns</span> <span class="o">=</span> <span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">O_CREAT</span><span class="p">)</span>

<span class="c1"># the same as above, the default behaviour</span>
<span class="n">netns</span> <span class="o">=</span> <span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>To remove a network namespace:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">NetNS</span>
<span class="n">netns</span> <span class="o">=</span> <span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">)</span>
<span class="n">netns</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">netns</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
</pre></div>
</div>
<p>One should stop it first with <cite>close()</cite>, and only after that
run <cite>remove()</cite>.</p>
<dl class="class">
<dt id="pyroute2.netns.nslink.NetNS">
<em class="property">class </em><code class="descclassname">pyroute2.netns.nslink.</code><code class="descname">NetNS</code><span class="sig-paren">(</span><em>netns</em>, <em>flags=64</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.nslink.NetNS" title="Permalink to this definition">¶</a></dt>
<dd><p>NetNS is the IPRoute API with network namespace support.</p>
<p><strong>Why not IPRoute?</strong></p>
<p>The task to run netlink commands in some network namespace, being in
another network namespace, requires the architecture, that differs
too much from a simple Netlink socket.</p>
<p>NetNS starts a proxy process in a network namespace and uses
<cite>multiprocessing</cite> communication channels between the main and the proxy
processes to route all <cite>recv()</cite> and <cite>sendto()</cite> requests/responses.</p>
<p><strong>Any specific API calls?</strong></p>
<p>Nope. <cite>NetNS</cite> supports all the same, that <cite>IPRoute</cite> does, in the same
way. It provides full <cite>socket</cite>-compatible API and can be used in
poll/select as well.</p>
<p>The only difference is the <cite>close()</cite> call. In the case of <cite>NetNS</cite> it
is <strong>mandatory</strong> to close the socket before exit.</p>
<p><strong>NetNS and IPDB</strong></p>
<p>It is possible to run IPDB with NetNS:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">NetNS</span>
<span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">IPDB</span>

<span class="n">ip</span> <span class="o">=</span> <span class="n">IPDB</span><span class="p">(</span><span class="n">nl</span><span class="o">=</span><span class="n">NetNS</span><span class="p">(</span><span class="s1">&#39;somenetns&#39;</span><span class="p">))</span>
<span class="o">...</span>
<span class="n">ip</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
<p>Do not forget to call <cite>release()</cite> when the work is done. It will shut
down <cite>NetNS</cite> instance as well.</p>
<dl class="method">
<dt id="pyroute2.netns.nslink.NetNS.remove">
<code class="descname">remove</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.nslink.NetNS.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Try to remove this network namespace from the system.</p>
</dd></dl>

</dd></dl>

</div>
<span class="target" id="module-pyroute2.netns.process.proxy"></span><div class="section" id="nspopen">
<h1>NSPopen<a class="headerlink" href="#nspopen" title="Permalink to this headline">¶</a></h1>
<p>The <cite>NSPopen</cite> class has nothing to do with netlink at
all, but it is required to have a reasonable network
namespace support.</p>
<dl class="class">
<dt id="pyroute2.netns.process.proxy.NSPopen">
<em class="property">class </em><code class="descclassname">pyroute2.netns.process.proxy.</code><code class="descname">NSPopen</code><span class="sig-paren">(</span><em>nsname</em>, <em>*argv</em>, <em>**kwarg</em><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.process.proxy.NSPopen" title="Permalink to this definition">¶</a></dt>
<dd><p>A proxy class to run <cite>Popen()</cite> object in some network namespace.</p>
<p>Sample to run <cite>ip ad</cite> command in <cite>nsname</cite> network namespace:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">nsp</span> <span class="o">=</span> <span class="n">NSPopen</span><span class="p">(</span><span class="s1">&#39;nsname&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;ip&#39;</span><span class="p">,</span> <span class="s1">&#39;ad&#39;</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">nsp</span><span class="o">.</span><span class="n">communicate</span><span class="p">())</span>
<span class="n">nsp</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="n">nsp</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
<p>The <cite>NSPopen</cite> class was intended to be a drop-in replacement
for the <cite>Popen</cite> class, but there are still some important
differences.</p>
<p>The <cite>NSPopen</cite> object implicitly spawns a child python process
to be run in the background in a network namespace. The target
process specified as the argument of the <cite>NSPopen</cite> will be
started in its turn from this child. Thus all the fd numbers
of the running <cite>NSPopen</cite> object are meaningless in the context
of the main process. Trying to operate on them, one will get
‘Bad file descriptor’ in the best case or a system call working
on a wrong file descriptor in the worst case. A possible
solution would be to transfer file descriptors between the
<cite>NSPopen</cite> object and the main process, but it is not implemented
yet.</p>
<p>The process’ diagram for <cite>NSPopen(‘test’, [‘ip’, ‘ad’])</cite>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>+---------------------+     +--------------+     +------------+
| main python process |&lt;---&gt;| child python |&lt;---&gt;| netns test |
| NSPopen()           |     | Popen()      |     | $ ip ad    |
+---------------------+     +--------------+     +------------+
</pre></div>
</div>
<p>As a workaround for the issue with file descriptors, some
additional methods are available on file objects <cite>stdin</cite>,
<cite>stdout</cite> and <cite>stderr</cite>. E.g., one can run fcntl calls:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">fcntl</span> <span class="k">import</span> <span class="n">F_GETFL</span>
<span class="kn">from</span> <span class="nn">pyroute2</span> <span class="k">import</span> <span class="n">NSPopen</span>
<span class="kn">from</span> <span class="nn">subprocess</span> <span class="k">import</span> <span class="n">PIPE</span>

<span class="n">proc</span> <span class="o">=</span> <span class="n">NSPopen</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;my_program&#39;</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
<span class="n">flags</span> <span class="o">=</span> <span class="n">proc</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">fcntl</span><span class="p">(</span><span class="n">F_GETFL</span><span class="p">)</span>
</pre></div>
</div>
<p>In that way one can use <cite>fcntl()</cite>, <cite>ioctl()</cite>, <cite>flock()</cite> and
<cite>lockf()</cite> calls.</p>
<p>Another additional method is <cite>release()</cite>, which can be used to
explicitly stop the proxy process and release all the resources.</p>
<dl class="method">
<dt id="pyroute2.netns.process.proxy.NSPopen.release">
<code class="descname">release</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pyroute2.netns.process.proxy.NSPopen.release" title="Permalink to this definition">¶</a></dt>
<dd><p>Explicitly stop the proxy process and release all the
resources. The <cite>NSPopen</cite> object can not be used after
the <cite>release()</cite> call.</p>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Netns management overview</a><ul>
<li><a class="reference internal" href="#move-an-interface-to-a-namespace">Move an interface to a namespace</a></li>
<li><a class="reference internal" href="#manage-interfaces-within-a-netns">Manage interfaces within a netns</a></li>
<li><a class="reference internal" href="#spawn-a-process-within-a-netns">Spawn a process within a netns</a></li>
<li><a class="reference internal" href="#list-set-create-and-remove-netns">List, set, create and remove netns</a></li>
</ul>
</li>
<li><a class="reference internal" href="#netns">NetNS</a></li>
<li><a class="reference internal" href="#nspopen">NSPopen</a></li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="ipset.html"
                        title="previous chapter">IPSet module</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="mpls.html"
                        title="next chapter">MPLS howto</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/netns.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </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="mpls.html" title="MPLS howto"
             >next</a> |</li>
        <li class="right" >
          <a href="ipset.html" title="IPSet module"
             >previous</a> |</li>
        <li class="nav-item"><a href="http://pyroute2.org">Project home</a> &#187;</li>
        <li class="nav-item nav-item-0"><a href="index.html">pyroute2 0.5.2 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2013, Peter V. Saveliev.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.2.
    </div>
  </body>
</html>