Ë Šê0ém5òãóΗdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZmZdd lmZmZmZej(e«ZGd „d e«ZGd „d ee«Zy)ae NetNS objects ============= 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. NetNS supports standard IPRoute API, so can be used instead of IPRoute, e.g., in IPDB:: # start the main network settings database: ipdb_main = IPDB() # start the same for a netns: ipdb_test = IPDB(nl=NetNS('test')) # create VETH ipdb_main.create(ifname='v0p0', kind='veth', peer='v0p1').commit() # move peer VETH into the netns with ipdb_main.interfaces.v0p1 as veth: veth.net_ns_fd = 'test' # please keep in mind, that netns move clears all the settings # on a VETH interface pair, so one should run netns assignment # as a separate operation only # assign addresses # please notice, that `v0p1` is already in the `test` netns, # so should be accessed via `ipdb_test` with ipdb_main.interfaces.v0p0 as veth: veth.add_ip('172.16.200.1/24') veth.up() with ipdb_test.interfaces.v0p1 as veth: veth.add_ip('172.16.200.2/24') veth.up() Please review also the test code, under `tests/test_netns.py` for more examples. 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 `open(2)` system call:: # create a new netns or fail, if it already exists netns = NetNS('test', flags=os.O_CREAT | os.O_EXCL) # create a new netns or use existing one netns = NetNS('test', flags=os.O_CREAT) # the same as above, the default behaviour netns = NetNS('test') To remove a network namespace:: from pyroute2 import NetNS netns = NetNS('test') netns.close() netns.remove() One should stop it first with `close()`, and only after that run `remove()`. éN)Úpartial)ÚRTNL_API)ÚRTMGRP_DEFAULTS)Ú MarshalRtnl)ÚremoveÚsetnsé)Ú RemoteSocketÚServerÚ Transportcó—eZdZd„Zd„Zd„Zy)ÚFDc ó~—||_dD]1}t||ttt|«|j««Œ3y)N)ÚreadÚwriteÚclose)ÚfdÚsetattrrÚgetattrÚos)ÚselfrÚnames ú;/usr/lib/python3.12/site-packages/pyroute2/nslink/nslink.pyÚ__init__z FD.__init__Ts1€ØˆŒÛ.ˆDÜ �D˜$¤¬´°DÓ(9¸4¿7¹7Ó CÕ Dñ/ócó—|jS©N)r©rs rÚfilenoz FD.filenoYs €Ø�w‰wˆrcó—yr©rs rÚflushzFD.flush\s€ØrN)Ú__name__Ú __module__Ú __qualname__rrr"r!rrrrSs„òEò órrc󘇗eZdZdZej ddefˆfd„ Zd„Zd„Z e jfˆfd„ Z ˆfd„Z ˆfd„Zˆfd „Zd „Zd „ZˆxZS) ÚNetNSa8 NetNS is the IPRoute API with network namespace support. **Why not IPRoute?** 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. NetNS starts a proxy process in a network namespace and uses `multiprocessing` communication channels between the main and the proxy processes to route all `recv()` and `sendto()` requests/responses. **Any specific API calls?** Nope. `NetNS` supports all the same, that `IPRoute` does, in the same way. It provides full `socket`-compatible API and can be used in poll/select as well. The only difference is the `close()` call. In the case of `NetNS` it is **mandatory** to close the socket before exit. Nc óÜ•—||_||_|xs|}tj«D�cgc]}t t |««‘Œc}\}|_tj«D�cgc]}t t |««‘Œc}\|_}tj«|_ |jdk(r°|j«|j«|jj«|jj« t|j|j|¬« t)|j|j ||¬«tjd« |jj«|j j«t*t,|�_|||¬«||_t3j4|j«t7«|_ycc}wcc}w#t$rH} |j jd| dœ«tj| j «Yd} ~ �Œd} ~ wt"$rj} |j jdtt j$t'| «|j«dœ«tjd«Yd} ~ �Œ�d} ~ wwxYw#tjd«wxYw#t"$r|j«‚wxYw)Nr)ÚlibcÚinit)ÚstageÚerroréÿ)ÚtargetÚgroups)r/)ÚnetnsÚflagsrÚpiper rÚremote_trnsp_outÚremote_trnsp_inÚforkÚchildrÚfile_objrÚOSErrorÚsendÚ_exitÚerrnoÚ ExceptionÚECOMMÚstrr Úsuperr'rr.ÚatexitÚregisterrÚmarshal) rr0r1r.r)r/ÚxÚtrnsp_inÚ trnsp_outÚeÚ __class__s €rrzNetNS.__init__ys,ø€ðˆŒ ؈Œ Ø’˜5ˆÜEGÇWÁWÄYÓ*OÁYÀ¬9´R¸³UÕ+;ÀYÑ*OÑ'ˆ�$Ô'ÜEGÇWÁWÄYÓ*OÁYÀ¬9´R¸³UÕ+;ÀYÑ*OÑ'ˆÔ˜iä—W‘W“YˆŒ Ø �:‰:˜Š?à �N‰NÔ Ø �O‰OÔ Ø × Ñ × #Ñ #Ô %Ø × Ñ × $Ñ $Ô &ð Ü�d—j‘j $§*¡*°4Õ8ð ÜØ×(Ñ(Ø×)Ñ)Ø!Ø!õ ô—‘˜” ð Ø × Ñ × &Ñ &Ô (Ø × !Ñ !× 'Ñ 'Ô )Ü ”%˜Ñ '¨°)ÀFÐ 'Ô KØ ˆDŒKô �‰˜Ÿ ™ Ô#Ü"“}ˆ� ùò[+PùÚ*Oøôò "Ø×&Ñ&×+Ñ+°fÀqÑ,IÔJÜ—‘˜Ÿ™×!Ò!ûÜò à×)Ñ)×.Ñ.à%+Ü%,¬U¯[©[¼#¸a»&À$Ç*Á*Ó%Mñôô—‘˜— ’ ûð ûô&—‘˜• ûôò Ø �J‰JŒLØ ð úsI¬G'Á(G,Ä "G1Ä.#J6Å'AKÇ1 J3Ç:=H=È= J3É AJ.Ê.J3Ê6K ËK+cóN—t|«|j|j«Sr)Útyper0r1rs rÚclonez NetNS.clone³s€ØŒt�D‹z˜$Ÿ*™* d§j¡jÓ1Ð1rcóÜ—ttd«r tj|j«y tjj |jdif«y#t $rYywxYw)NÚ unregisterr!)Úhasattrr@rLrÚ _exithandlersrÚ ValueErrorrs rÚ_cleanup_atexitzNetNS._cleanup_atexit¶sT€Ü ”6˜<Ô (Ü × Ñ ˜dŸj™jÕ )ð Ü×$Ñ$×+Ñ+¨T¯Z©Z¸¸RÐ,@ÕAøÜò Ùð ús²,AÁ A+Á*A+cóä•—|j« tt|�|¬«y# |jj ddi«n#t $rYnwxYwtjd«YyxYw)N)Úcoder+Úshutdownz2forced shutdown procedure, clean up netns manually) rPr?r'rrEr9r<Úlogr,)rrRrGs €rrz NetNS.close¿sjø€Ø ×ÑÔð LÜ ”%˜Ñ $¨$Ð $Õ /øð Lð Ø—‘×#Ñ# W¨jÐ$9Õ:øÜò Ùð úä �I‰IÐJÖ Kús,“(¨A/«A ÁA/Á AÁA/ÁAÁA/cól•—|jdk7r|jd|«Stt|�|«S)z0Proxy the open_file method if we are the parent.rÚ open_file)r6Úproxyr?r'rV)rÚpathrGs €rrVzNetNS.open_fileËs1ø€à �:‰:˜Š?Ø—:‘:˜k¨4Ó0Ð 0ä”U˜DÑ+¨DÓ1Ð1rcól•—|jdk7r|jd|«Stt|�|«S)z1Proxy the close_file method if we are the parent.rÚ close_file)r6rWr?r'rZ)rrrGs €rrZzNetNS.close_fileÒs1ø€à �:‰:˜Š?Ø—:‘:˜l¨BÓ/Ð /ä”U˜DÑ,¨RÓ0Ð0rcóh•—|jdk7r|jd«Stt|�«S)z.Proxy the get_pid method if we are the parent.rÚget_pid)r6rWr?r'r\)rrGs €rr\z NetNS.get_pidÙs-ø€à �:‰:˜Š?Ø—:‘:˜iÓ(Ð (ä”U˜DÑ)Ó+Ð+rcó—yrr!rs rÚ post_initzNetNS.post_initàs€Ø rcó.—t|j«y)zG Try to remove this network namespace from the system. N)rr0rs rrz NetNS.removeãs€ô ˆt�z‰zÕr)r#r$r%Ú__doc__rÚO_CREATrrrJrPr;Ú ECONNRESETrrVrZr\r^rÚ __classcell__)rGs@rr'r'`sTø„ñð6�j‰jØØ Øõ 8%òt2òð×)Ñ)õ Lô2ô1ô,ò örr')r`r@r;ÚloggingrÚ functoolsrÚpyroute2.iprouterÚpyroute2.netlink.rtnlrÚpyroute2.netlink.rtnl.iprsocketrÚpyroute2.netnsrrÚremote.transportr r r Ú getLoggerr#rTÚobjectrr'r!rrÚrms]ðñ@óDÛ ÛÛ Ýå%Ý1Ý7ß(ç>Ñ>à€g×јÓ!€ô ˆô ôGˆH�lõGr