p4net.control¶
P4Runtime control plane: gRPC client, P4Info index, value codecs.
This package depends on the p4runtime PyPI distribution for the generated
proto stubs. Those stubs are descriptor-incompatible with the C++
google.protobuf runtime shipped in protobuf 4.x and later, so we set
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python here, before any p4 stubs
are imported transitively. This is the upstream-recommended workaround
until the p4runtime distribution is rebuilt against modern protoc.
__all__
module-attribute
¶
__all__ = ['AsyncOperationCancelledError', 'AsyncP4RuntimeClient', 'ConnectionError', 'CounterData', 'DuplicateEntryError', 'EncodingError', 'EntryNotFoundError', 'NoSuchActionError', 'NoSuchFieldError', 'NoSuchRegisterError', 'NoSuchTableError', 'NotPrimaryError', 'P4InfoIndex', 'P4RuntimeClient', 'P4RuntimeError', 'PipelineError', 'RegisterSpec', 'canonicalize', 'decode_int', 'decode_ipv4', 'decode_ipv6', 'decode_mac', 'encode_int', 'encode_ipv4', 'encode_mac', 'encode_value', 'format_exact', 'format_lpm', 'format_range', 'format_ternary', 'parse_lpm', 'parse_range', 'parse_ternary']
AsyncP4RuntimeClient ¶
Async parallel to :class:P4RuntimeClient. Stable in 1.x since 1.7.0.
Source code in src/p4net/control/async_client.py
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 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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | |
push_pipeline
async
¶
push_pipeline(p4info_bytes: bytes, json_bytes: bytes, *, action: str = 'VERIFY_AND_COMMIT', timeout: float = 10.0) -> None
Push a compiled pipeline (raw bytes).
Source code in src/p4net/control/async_client.py
list_table_entries ¶
list_table_entries(table: str | None = None, *, timeout: float = 5.0) -> AsyncIterator[dict[str, Any]]
Async iterator over decoded table entries.
Note: this is a regular def returning an AsyncIterator.
Use it as async for entry in client.list_table_entries(...).
Source code in src/p4net/control/async_client.py
read_counter
async
¶
read_counter(counter: str, index: int | None = None, *, timeout: float = 5.0) -> int | dict[int, int]
Read indirect counter. Returns packet_count only for simplicity.
Single-index returns the cell's packet_count (int). No index returns
{cell_index: packet_count}. The sync client returns the richer
CounterData dataclass; the async API keeps things simple per spec.
Source code in src/p4net/control/async_client.py
on_packet_in
async
¶
on_packet_in(handler: Callable[[bytes, dict[str, int]], Awaitable[None]]) -> Callable[[], Awaitable[None]]
Register an async PacketIn handler. Returns an async unsubscribe.
Source code in src/p4net/control/async_client.py
expect_packet_in
async
¶
Await the next PacketIn. Raises P4RuntimeError on timeout.
Source code in src/p4net/control/async_client.py
CounterData
dataclass
¶
P4RuntimeClient ¶
gRPC client for a single P4Runtime device.
Source code in src/p4net/control/client.py
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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | |
index
property
¶
The :class:P4InfoIndex for the currently-pushed pipeline.
Raises:
| Type | Description |
|---|---|
P4RuntimeError
|
if no pipeline has been set yet. |
is_connected ¶
connect ¶
Open the gRPC channel, start the StreamChannel, do master arbitration.
Source code in src/p4net/control/client.py
disconnect ¶
set_pipeline_config ¶
set_pipeline_config(*, bmv2_json: Path, p4info: Path, action: str = 'VERIFY_AND_COMMIT', timeout: float = 10.0) -> None
Push a compiled pipeline to the device.
Source code in src/p4net/control/client.py
get_pipeline_config ¶
Read the current pipeline back from the device.
Source code in src/p4net/control/client.py
insert_table_entry ¶
insert_table_entry(table: str, match: Mapping[str, object], action: str, params: Mapping[str, object] | None = None, *, priority: int | None = None, timeout: float = 5.0) -> None
Insert a new entry into table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Fully qualified table name (e.g. |
required |
match
|
Mapping[str, object]
|
|
required |
action
|
str
|
Fully qualified action name. |
required |
params
|
Mapping[str, object] | None
|
Action parameters keyed by P4 param name. |
None
|
priority
|
int | None
|
Required for tables with ternary or range keys. |
None
|
timeout
|
float
|
Per-call gRPC deadline in seconds. |
5.0
|
Raises:
| Type | Description |
|---|---|
DuplicateEntryError
|
if an entry with the same key already exists. |
EncodingError
|
if any field can't be encoded. |
Source code in src/p4net/control/client.py
modify_table_entry ¶
modify_table_entry(table: str, match: Mapping[str, object], action: str, params: Mapping[str, object] | None = None, *, priority: int | None = None, timeout: float = 5.0) -> None
Modify an existing entry in table. Same arguments as
:meth:insert_table_entry. Raises :class:EntryNotFoundError
if no matching entry exists.
Source code in src/p4net/control/client.py
delete_table_entry ¶
delete_table_entry(table: str, match: Mapping[str, object], *, priority: int | None = None, timeout: float = 5.0) -> None
Delete an entry from table by match key.
Raises:
| Type | Description |
|---|---|
EntryNotFoundError
|
if no matching entry exists. |
Source code in src/p4net/control/client.py
list_table_entries ¶
Return decoded entries for one table, or all tables if table is None.
The byte values in each entry's match mapping are returned in
P4Runtime canonical form (P4Runtime spec §8.4): they may be shorter
than the bitwidth-rounded width because leading zero bytes are
stripped. They round-trip correctly through insert_table_entry,
modify_table_entry, and delete_table_entry because
encode_value accepts shorter bytes inputs.
Source code in src/p4net/control/client.py
clear_table ¶
Delete every entry from table. Returns the count deleted.
Source code in src/p4net/control/client.py
read_counter ¶
read_counter(counter: str, index: int | None = None, *, timeout: float = 5.0) -> CounterData | dict[int, CounterData]
Read one or all populated cells of an indirect counter.
Source code in src/p4net/control/client.py
reset_counter ¶
Zero one or all indices of an indirect counter.
Source code in src/p4net/control/client.py
write_register ¶
Write a single cell of a P4 register.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified P4 register name
(e.g. |
required |
index
|
int
|
Cell index in the register array. Must be in range
|
required |
value
|
int
|
Integer value to write. Must fit in the register's bitwidth. |
required |
Raises:
| Type | Description |
|---|---|
NoSuchRegisterError
|
if the register doesn't exist. |
EncodingError
|
if |
P4RuntimeError
|
if the underlying control channel returns an error or this client has no Thrift address configured. |
Source code in src/p4net/control/client.py
read_register ¶
Read a P4 register.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified P4 register name. |
required |
index
|
int | None
|
Cell index, or |
None
|
Returns:
| Type | Description |
|---|---|
int | list[int]
|
If |
int | list[int]
|
If |
int | list[int]
|
position |
int | list[int]
|
initializes register elements to zero). |
Raises:
| Type | Description |
|---|---|
NoSuchRegisterError
|
if the register doesn't exist. |
EncodingError
|
if |
P4RuntimeError
|
if the underlying control channel returns an error or this client has no Thrift address configured. |
Source code in src/p4net/control/client.py
add_multicast_group ¶
Create a multicast group with one replica per port (instance=1).
Source code in src/p4net/control/client.py
modify_multicast_group ¶
Replace the replica list of an existing multicast group.
Source code in src/p4net/control/client.py
delete_multicast_group ¶
list_multicast_groups ¶
Return {group_id: [egress_port, ...]}.
Replica instance numbers are flattened away — each port appears once per replica regardless of its instance value (we always write instance=1 ourselves; foreign instance values are still listed but not exposed in this dict shape).
Source code in src/p4net/control/client.py
send_packet_out ¶
send_packet_out(payload: bytes, metadata: Mapping[str, object] | None = None, *, timeout: float = 5.0) -> None
Send a PacketOut over the StreamChannel.
payload is the full packet to inject — controller headers are
rebuilt from metadata per the loaded P4Info. PacketOut is
fire-and-forget in P4Runtime; this method does not wait for a switch
response. timeout is reserved for future flow-control limits and
currently only bounds the queue put.
Source code in src/p4net/control/client.py
on_packet_in ¶
Register a PacketIn handler. Returns a deregister function.
Handlers run on the StreamChannel consumer thread. Multiple handlers are invoked in registration order; an exception from one is logged and does not prevent later handlers from running. The returned deregister function tolerates double-unregister silently.
Source code in src/p4net/control/client.py
expect_packet_in ¶
Block until the next PacketIn arrives. Raises P4RuntimeError on timeout.
Source code in src/p4net/control/client.py
AsyncOperationCancelledError ¶
Bases: P4RuntimeError
An async client operation was cancelled mid-flight.
Raised by :class:p4net.control.AsyncP4RuntimeClient when an in-flight
RPC is cancelled (typically because the owning task was cancelled, or
because disconnect() is called while another coroutine is awaiting
a response). Subclasses :class:P4RuntimeError so existing
except P4RuntimeError handlers still cover it, but lets cancellation
sites distinguish a clean cancel from a connection failure.
Stable in p4net 1.x since version 1.7.0 — same stability tier as
AsyncP4RuntimeClient.
Source code in src/p4net/control/exceptions.py
ConnectionError ¶
Bases: P4RuntimeError
Failure to open the gRPC channel or complete master arbitration.
DuplicateEntryError ¶
Bases: P4RuntimeError
Insert failed because the entry already exists.
EncodingError ¶
Bases: P4RuntimeError
A value could not be encoded for the declared field bitwidth or match type.
EntryNotFoundError ¶
Bases: P4RuntimeError
Modify or delete failed because the entry does not exist.
NoSuchActionError ¶
Bases: P4RuntimeError
Referenced action is not present in the current P4Info.
NoSuchFieldError ¶
Bases: P4RuntimeError
Referenced match field or action parameter is not present.
NoSuchRegisterError ¶
Bases: P4RuntimeError
Referenced register is not present in the current P4Info.
NoSuchTableError ¶
Bases: P4RuntimeError
Referenced table is not present in the current P4Info.
NotPrimaryError ¶
Bases: P4RuntimeError
The local client is not the primary controller for this device.
P4RuntimeError ¶
Bases: P4NetError
Base class for P4Runtime client failures.
PipelineError ¶
Bases: P4RuntimeError
SetForwardingPipelineConfig was rejected by the switch.
P4InfoIndex ¶
Indexed view of a P4Info message with name → id and encoding helpers.
Source code in src/p4net/control/p4info_index.py
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 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 | |
from_file
classmethod
¶
from_bytes
classmethod
¶
Parse from text or binary protobuf bytes.
Source code in src/p4net/control/p4info_index.py
table_id ¶
Return the numeric table ID for name. Raises :class:NoSuchTableError.
Source code in src/p4net/control/p4info_index.py
action_id ¶
Return the numeric action ID for name. Raises :class:NoSuchActionError.
Source code in src/p4net/control/p4info_index.py
counter_id ¶
Return the numeric counter ID for name. Raises :class:P4RuntimeError.
Source code in src/p4net/control/p4info_index.py
table_name ¶
Inverse of :meth:table_id.
Source code in src/p4net/control/p4info_index.py
action_name ¶
Inverse of :meth:action_id.
Source code in src/p4net/control/p4info_index.py
counter_name ¶
Inverse of :meth:counter_id.
Source code in src/p4net/control/p4info_index.py
register_by_name ¶
Look up a register by its fully qualified P4 name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified P4 name (e.g. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
RegisterSpec
|
class: |
Raises:
| Type | Description |
|---|---|
NoSuchRegisterError
|
if no register with that name exists. |
Source code in src/p4net/control/p4info_index.py
table_requires_priority ¶
True iff the table has a TERNARY or RANGE match field.
Source code in src/p4net/control/p4info_index.py
multicast_group_id_unused ¶
encode_match ¶
Build a list of P4Runtime FieldMatch protos for the given table.
Source code in src/p4net/control/p4info_index.py
decode_match ¶
Inverse of encode_match: render raw match bytes as human strings.
For each (field_name, raw_value) pair the field's bitwidth and
match type are looked up in the P4Info, then the value is formatted:
- 32-bit fields → IPv4 dotted-quad (with
/<plen>for LPM,&<mask>for TERNARY,[<lo>,<hi>]for RANGE). - 48-bit fields → MAC
xx:xx:xx:xx:xx:xx(with the same combinators). - Other widths → decimal int (with the same combinators).
Width is taken from P4Info; the bytes may be canonical (shorter than width-rounded) and are zero-extended on the high side before decoding.
Source code in src/p4net/control/p4info_index.py
encode_action ¶
Build a P4Runtime Action proto.
Source code in src/p4net/control/p4info_index.py
packet_in_metadata_schema ¶
Return [(name, bitwidth), ...] for the packet_in controller header.
Empty list if the program declares no @controller_header("packet_in").
Source code in src/p4net/control/p4info_index.py
packet_out_metadata_schema ¶
Return [(name, bitwidth), ...] for the packet_out controller header.
Empty list if the program declares no @controller_header("packet_out").
Source code in src/p4net/control/p4info_index.py
encode_packet_out_metadata ¶
Encode controller metadata for a PacketOut.
Each (key, value) pair is matched against the packet_out schema;
values go through encode_value with the schema's bitwidth, then
wrapped in a PacketMetadata proto with the corresponding numeric id
(P4Runtime uses ids on the wire, not field names). Missing keys are
populated with zero-valued bytes of the correct width. Raises
NoSuchFieldError on unknown keys, EncodingError on width
overflow.
Source code in src/p4net/control/p4info_index.py
decode_packet_in_metadata ¶
Decode PacketIn controller metadata into {field_name: int}.
Unknown ids (e.g. switch running a different P4Info than the controller has loaded) are silently dropped; the dispatcher logs them at DEBUG.
Source code in src/p4net/control/p4info_index.py
RegisterSpec
dataclass
¶
Metadata for a P4 register declared in the pipeline.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
P4Runtime register ID, used in WriteRequest / ReadRequest. |
name |
str
|
Fully qualified P4 name (e.g. |
bitwidth |
int
|
Width of each register element in bits. |
size |
int
|
Number of elements in the register array. |
Source code in src/p4net/control/p4info_index.py
canonicalize ¶
Return P4Runtime canonical form: strip leading zeros; zero -> b'\x00'.
decode_int ¶
Inverse of encode_int. Accepts canonical or full-width input.
Source code in src/p4net/control/codec.py
decode_ipv4 ¶
Inverse of encode_ipv4. Accepts canonical (short) or 4-byte input.
Source code in src/p4net/control/codec.py
decode_ipv6 ¶
Decode up to 16 canonical bytes as an IPv6 condensed-form string.
Inputs shorter than 16 bytes are zero-extended on the high side, matching
P4Runtime canonical encoding (§8.4). The condensed form is whatever
:class:ipaddress.IPv6Address.__str__ produces (e.g. "fd00::1").
Source code in src/p4net/control/codec.py
decode_mac ¶
Inverse of encode_mac. Accepts canonical (short) or 6-byte input.
Source code in src/p4net/control/codec.py
encode_int ¶
Encode an integer as canonical big-endian bytes for the given bitwidth.
The returned slice is the minimum byte width that holds bitwidth bits.
Raises EncodingError if value is negative or does not fit.
Source code in src/p4net/control/codec.py
encode_ipv4 ¶
Encode '10.0.0.1' as 4 big-endian bytes.
Source code in src/p4net/control/codec.py
encode_mac ¶
Encode 'aa:bb:cc:dd:ee:ff' as 6 bytes.
Source code in src/p4net/control/codec.py
encode_value ¶
Auto-dispatch encode for a single field value.
Source code in src/p4net/control/codec.py
format_exact ¶
format_lpm ¶
Render an LPM (value, prefix_len) tuple as <addr>/<plen>.
format_range ¶
Render a RANGE (low, high) tuple as [<low>,<high>].
format_ternary ¶
Render a TERNARY (value, mask) tuple as <value>&<mask>.
parse_lpm ¶
Accept '10.0.0.0/24' or ('10.0.0.0', 24). Returns (encoded_value, prefix_len).
Source code in src/p4net/control/codec.py
parse_range ¶
Accept (low, high). Returns (encoded_low, encoded_high).
Source code in src/p4net/control/codec.py
parse_ternary ¶
parse_ternary(value: tuple[str | int | bytes, str | int | bytes], bitwidth: int) -> tuple[bytes, bytes]
Accept ('value', 'mask'). Returns (encoded_value, encoded_mask).