p4net.runtime¶
Runtime primitives: namespaces, links, traffic shaping.
__all__
module-attribute
¶
__all__ = ['BMv2NotFoundError', 'BMv2StartupError', 'BMv2Switch', 'LinkError', 'NSProcess', 'NamespaceError', 'NetworkNamespace', 'P4NetError', 'PrivilegeError', 'TcError', 'VethPair', 'apply_netem', 'clear_qdisc', 'disable_ipv6', 'enable_ipv6']
BMv2Switch ¶
Process lifecycle wrapper for a single simple_switch_grpc instance.
Source code in src/p4net/runtime/bmv2.py
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 | |
thrift_port
property
¶
Thrift bind port for simple_switch_CLI and register operations.
grpc_port
property
¶
gRPC port the P4Runtime server bound to (host portion is grpc_bind_addr).
boot_timestamp_us
property
¶
Wall-clock microseconds since Unix epoch when this BMv2 process started.
None if the switch has not been started yet, or has been stopped
since its last start. Captured immediately before subprocess.Popen,
so drift from BMv2's internal clock zero is bounded by Popen overhead
(sub-millisecond on a typical Linux host).
Combined with INT shim ingress_timestamp_us to derive wall-clock
arrival time across multiple switches::
wall_clock_us = bmv2.boot_timestamp_us + shim.ingress_timestamp_us
start ¶
Spawn the simple_switch_grpc process. Non-blocking.
Source code in src/p4net/runtime/bmv2.py
wait_until_ready ¶
Block until the gRPC port accepts a connection, or fail.
Source code in src/p4net/runtime/bmv2.py
stop ¶
Send SIGTERM, escalate to SIGKILL on timeout. Idempotent.
Source code in src/p4net/runtime/bmv2.py
kill ¶
Send SIGKILL immediately. Idempotent.
Source code in src/p4net/runtime/bmv2.py
is_running ¶
returncode ¶
BMv2NotFoundError ¶
Bases: P4NetError
Raised when the configured simple_switch_grpc binary is not on PATH.
BMv2StartupError ¶
Bases: P4NetError
Raised when a BMv2 process fails to start, fails to become ready, or exits unexpectedly during startup.
LinkError ¶
Bases: P4NetError
Failure creating, configuring, or destroying a network link.
NamespaceError ¶
Bases: P4NetError
Failure creating, destroying, or operating in a network namespace.
P4NetError ¶
PrivilegeError ¶
Bases: P4NetError
Raised when the operation requires root or CAP_NET_ADMIN and the caller has neither.
TcError ¶
Bases: P4NetError
Failure configuring traffic control / netem.
VethPair ¶
A veth pair with explicit lifecycle and per-side namespace tracking.
Source code in src/p4net/runtime/link.py
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 | |
name_of ¶
namespace_of ¶
create ¶
Create the kernel veth pair via netlink. Both ends start in root netns.
Raises:
| Type | Description |
|---|---|
LinkError
|
if either interface already exists or the pair has already been created. |
Source code in src/p4net/runtime/link.py
destroy ¶
Delete the veth pair (both ends, regardless of namespace).
Source code in src/p4net/runtime/link.py
move_to_namespace ¶
Move side of the pair into ns (or back to root if None).
Source code in src/p4net/runtime/link.py
set_up ¶
set_down ¶
set_address ¶
Assign an IPv4 CIDR (e.g. "10.0.0.1/24") to side.
Source code in src/p4net/runtime/link.py
set_address6 ¶
Assign an IPv6 CIDR (e.g. "fd00::1/64") to one side of the pair.
Implementation: same pyroute2 IPRoute path as set_address, but with
family=socket.AF_INET6. Validates the input via
ipaddress.IPv6Interface and rejects IPv4 strings (their /0
through /32 masks would parse as IPv6Interface only by accident).
Raises :class:LinkError on assignment failure.
Source code in src/p4net/runtime/link.py
set_mtu ¶
Set the MTU on side (clamped to [68, 65535]).
Source code in src/p4net/runtime/link.py
set_mac ¶
Override the MAC on side (canonical XX:XX:XX:XX:XX:XX).
Source code in src/p4net/runtime/link.py
NetworkNamespace ¶
A named Linux network namespace with explicit lifecycle.
Use create() / destroy() directly, or use the instance as a context
manager. exec() runs a command inside the namespace and waits for it;
popen() returns a long-running process handle.
Source code in src/p4net/runtime/netns.py
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 | |
create ¶
Create the namespace.
Raises:
| Type | Description |
|---|---|
NamespaceError
|
if a namespace with the same name already exists. |
Source code in src/p4net/runtime/netns.py
destroy ¶
Remove the namespace.
Raises:
| Type | Description |
|---|---|
NamespaceError
|
if the namespace does not exist. |
Source code in src/p4net/runtime/netns.py
exec ¶
exec(argv: Sequence[str], *, timeout: float | None = None, check: bool = True, capture_output: bool = False, env: Mapping[str, str] | None = None) -> subprocess.CompletedProcess[bytes]
Run argv inside this namespace and wait for completion.
Mirrors subprocess.run semantics: returns a CompletedProcess,
raises subprocess.CalledProcessError if check and the exit
status is non-zero, raises subprocess.TimeoutExpired on timeout.
Source code in src/p4net/runtime/netns.py
popen ¶
popen(argv: Sequence[str], *, env: Mapping[str, str] | None = None, stdout: int | IO[Any] | None = None, stderr: int | IO[Any] | None = None, stdin: int | IO[Any] | None = None) -> NSProcess
Spawn a long-running process inside this namespace.
Returns an NSProcess wrapping a regular subprocess.Popen of
ip netns exec <name> <argv>. The wrapper exposes a
subprocess.Popen-compatible surface (pid, poll, wait,
terminate, kill, close).
Source code in src/p4net/runtime/netns.py
NSProcess ¶
A process running inside a NetworkNamespace.
Wraps a regular subprocess.Popen returned by NetworkNamespace.popen.
The forwarded API (pid, poll, wait, terminate, kill) mirrors
subprocess.Popen. close() is preserved as a no-op so callers from
earlier phases keep working unchanged: there is no namespace-side
handle to release because the wrapped process is a regular child of
ip netns exec.
Source code in src/p4net/runtime/netns.py
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 | |
poll ¶
wait ¶
terminate ¶
kill ¶
close ¶
Idempotent no-op preserved for API stability.
Earlier phases needed this method to release a pyroute2.NSPopen
helper; with a regular subprocess.Popen there is nothing to
release, but the method is kept so callers compiled against the
old API don't break. Calling close more than once is safe.
Source code in src/p4net/runtime/netns.py
disable_ipv6 ¶
Set net.ipv6.conf.<iface>.disable_ipv6=1 inside ns.
Idempotent — sysctl -w to an already-set value is harmless. Pass
ns=None to target the root namespace.
Source code in src/p4net/runtime/sysctl.py
enable_ipv6 ¶
enable_ipv6(ns: NetworkNamespace | None, iface: str, *, accept_ra: bool = False, autoconf: bool = False) -> None
Set disable_ipv6=0 plus accept_ra and autoconf on iface.
Defaults turn off Router Advertisement handling and SLAAC autoconfiguration
so the host only carries addresses we explicitly assign. Set
accept_ra=True or autoconf=True if you want kernel-level
auto-addressing on this interface.
Source code in src/p4net/runtime/sysctl.py
apply_netem ¶
apply_netem(ns: NetworkNamespace | None, iface: str, *, rate: str | None = None, delay: str | None = None, jitter: str | None = None, loss_pct: float | None = None) -> None
Apply a netem root qdisc to iface inside ns (or root if ns is None).
Uses tc qdisc replace so calling this function repeatedly is safe.
At least one of rate, delay, jitter, loss_pct must be set.
jitter requires delay.
Source code in src/p4net/runtime/tc.py
clear_qdisc ¶
Remove the root qdisc on iface. Idempotent: no-op if none is set.