p4net.topo¶
Topology description layer: pure data classes, no syscalls.
__all__
module-attribute
¶
TopologyError ¶
Bases: P4NetError
Topology validation or construction failure.
Host
dataclass
¶
A network host with optional primary IP, MAC, and default route.
IPv4 lives in ip / default_route; IPv6 lives in ip6 /
default_route6. Either, neither, or both may be set.
Source code in src/p4net/topo/host.py
Link
dataclass
¶
A bidirectional veth pair between two nodes, with optional impairments.
Symmetric impairments (bandwidth / delay / jitter /
loss_pct) apply equally to both veth sides. Per-direction overrides
(*_a_to_b / *_b_to_a) shape only one direction; mixing a symmetric
field and a matching asymmetric field for the same parameter is rejected.
Per-direction additive fields (*_a_to_b_extra / *_b_to_a_extra)
layer on top of the symmetric base. They require the symmetric base to be
set and are mutually exclusive with the per-direction override on the
same dimension. Bandwidth has no _extra form because additive
bandwidth lacks clean physical semantics.
Source code in src/p4net/topo/link.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 | |
LinkEndpoint
dataclass
¶
One end of a Link, attached to a host or switch.
ip and ip6 are link-level overrides that apply only on host
endpoints; switch endpoints don't carry L3 addresses.
Source code in src/p4net/topo/link.py
P4Switch
dataclass
¶
A P4-programmable switch backed by BMv2 simple_switch_grpc.
Source code in src/p4net/topo/switch.py
Topology ¶
A mutable builder for a topology description.
Source code in src/p4net/topo/topology.py
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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | |
node ¶
Look up a node by name.
Returns:
| Name | Type | Description |
|---|---|---|
The |
NodeKind
|
class: |
Raises:
| Type | Description |
|---|---|
TopologyError
|
if no node with that name exists. |
Source code in src/p4net/topo/topology.py
add_host ¶
add_host(name: str, *, ip: str | None = None, mac: str | None = None, default_route: str | None = None, ip6: str | None = None, default_route6: str | None = None) -> Host
Append a :class:Host to the topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Host name; must be unique across hosts and switches. |
required |
ip
|
str | None
|
IPv4 CIDR (e.g. |
None
|
mac
|
str | None
|
MAC address (e.g. |
None
|
default_route
|
str | None
|
IPv4 default-route gateway address. |
None
|
ip6
|
str | None
|
IPv6 CIDR (e.g. |
None
|
default_route6
|
str | None
|
IPv6 default-route gateway address. |
None
|
Returns:
| Type | Description |
|---|---|
Host
|
The newly created :class: |
Source code in src/p4net/topo/topology.py
add_switch ¶
add_switch(name: str, p4_src: Path, *, arch: str = 'v1model', device_id: int | None = None, grpc_port: int | None = None, thrift_port: int | None = None, cpu_port: int | None = None, log_level: str = 'info', pcap_enabled: bool = True) -> P4Switch
Append a :class:P4Switch to the topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Switch name; must be unique across hosts and switches. |
required |
p4_src
|
Path
|
Path to the P4 source file the switch should run. |
required |
arch
|
str
|
P4 architecture name; only |
'v1model'
|
device_id
|
int | None
|
P4Runtime device ID. Auto-assigned starting at 0. |
None
|
grpc_port
|
int | None
|
gRPC bind port. Auto-assigned starting at 50051. |
None
|
thrift_port
|
int | None
|
Thrift bind port. Auto-assigned starting at 9090. |
None
|
cpu_port
|
int | None
|
CPU port number for controller punt (optional). |
None
|
log_level
|
str
|
BMv2 log level passed via |
'info'
|
pcap_enabled
|
bool
|
Per-port pcap capture toggle. |
True
|
Returns:
| Type | Description |
|---|---|
P4Switch
|
The newly created :class: |
Source code in src/p4net/topo/topology.py
add_link ¶
add_link(a: NodeRef, b: NodeRef, *, port_a: int | None = None, port_b: int | None = None, ip_a: str | None = None, ip_b: str | None = None, mac_a: str | None = None, mac_b: str | None = None, ip6_a: str | None = None, ip6_b: str | None = None, bandwidth: str | None = None, delay: str | None = None, jitter: str | None = None, loss_pct: float | None = None, mtu: int | None = None, bandwidth_a_to_b: str | None = None, bandwidth_b_to_a: str | None = None, delay_a_to_b: str | None = None, delay_b_to_a: str | None = None, jitter_a_to_b: str | None = None, jitter_b_to_a: str | None = None, loss_pct_a_to_b: float | None = None, loss_pct_b_to_a: float | None = None, delay_a_to_b_extra: str | None = None, delay_b_to_a_extra: str | None = None, jitter_a_to_b_extra: str | None = None, jitter_b_to_a_extra: str | None = None, loss_pct_a_to_b_extra: float | None = None, loss_pct_b_to_a_extra: float | None = None) -> Link
Append a :class:Link between two nodes.
Endpoint sides a and b are anchored by argument order;
per-direction impairment fields like delay_a_to_b shape only
the direction from the a-side veth toward the b side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
NodeRef
|
First endpoint (host or switch name, or a node object). |
required |
b
|
NodeRef
|
Second endpoint. |
required |
port_a
|
int | None
|
Port number on the |
None
|
port_b
|
int | None
|
Port number on the |
None
|
ip_a
|
str | None
|
IPv4 CIDR override for the |
None
|
ip_b
|
str | None
|
IPv4 CIDR override for the |
None
|
mac_a
|
str | None
|
MAC override for the |
None
|
mac_b
|
str | None
|
MAC override for the |
None
|
ip6_a
|
str | None
|
IPv6 CIDR override for the |
None
|
ip6_b
|
str | None
|
IPv6 CIDR override for the |
None
|
bandwidth
|
str | None
|
Symmetric link-rate cap (e.g. |
None
|
delay
|
str | None
|
Symmetric one-way delay (e.g. |
None
|
jitter
|
str | None
|
Symmetric jitter; requires |
None
|
loss_pct
|
float | None
|
Symmetric per-packet loss in [0.0, 100.0]. |
None
|
mtu
|
int | None
|
Link MTU (clamped to [68, 65535]). |
None
|
bandwidth_a_to_b
|
str | None
|
Per-direction bandwidth, |
None
|
bandwidth_b_to_a
|
str | None
|
Per-direction bandwidth, |
None
|
delay_a_to_b
|
str | None
|
Per-direction delay, |
None
|
delay_b_to_a
|
str | None
|
Per-direction delay, |
None
|
jitter_a_to_b
|
str | None
|
Per-direction jitter, |
None
|
jitter_b_to_a
|
str | None
|
Per-direction jitter, |
None
|
loss_pct_a_to_b
|
float | None
|
Per-direction loss, |
None
|
loss_pct_b_to_a
|
float | None
|
Per-direction loss, |
None
|
delay_a_to_b_extra
|
str | None
|
Additional delay added on top of the symmetric
|
None
|
delay_b_to_a_extra
|
str | None
|
Same as above, |
None
|
jitter_a_to_b_extra
|
str | None
|
Additional jitter on top of symmetric
|
None
|
jitter_b_to_a_extra
|
str | None
|
Same as above, |
None
|
loss_pct_a_to_b_extra
|
float | None
|
Additional loss percent on top of symmetric
|
None
|
loss_pct_b_to_a_extra
|
float | None
|
Same as above, |
None
|
Returns:
| Type | Description |
|---|---|
Link
|
The newly created :class: |
Raises:
| Type | Description |
|---|---|
TopologyError
|
on invalid parameter combinations or unresolved endpoints. |
Source code in src/p4net/topo/topology.py
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 | |
neighbors_of ¶
Return every link involving name, as (link, near, far).
Source code in src/p4net/topo/topology.py
port_assignments ¶
Map of switch port number to the FAR endpoint connected on that port.
Source code in src/p4net/topo/topology.py
validate ¶
Raise TopologyError listing every internal-consistency problem found.
Source code in src/p4net/topo/topology.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
to_graphviz ¶
Render the topology as a Graphviz DOT graph string.
Hosts are ellipses labelled with name and primary IP(s); switches are
boxes labelled with name and gRPC port. Edges are drawn with
arrowhead=none to keep the rendering version-stable across
graphviz releases. layout controls rankdir and must be one
of "LR", "RL", "TB", "BT".
Source code in src/p4net/topo/topology.py
render_graphviz ¶
Render via the system dot binary to output_path.
format is forwarded to dot -T<format> (png, svg, pdf, dot).
For format="dot" the source is written verbatim and dot is
not invoked, so this path works without graphviz installed.
Raises :class:TopologyError if dot is missing or the render
fails.
Source code in src/p4net/topo/topology.py
to_dict ¶
Return a JSON-serialisable snapshot of the topology.
Source code in src/p4net/topo/topology.py
from_dict
classmethod
¶
Reconstruct a :class:Topology from a :meth:to_dict payload.