Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

テンプレートスキーマ

テンプレートは 1 つのストリームを記述する YAML ファイル (.wpg.yaml。.wpg.json の JSON も同等) です。レイヤスタック、フィールドを変化させるジェネレータ、送出プロファイルから成ります。機械可読な定義は docs/schema/template.schema.json で、wpg template schema でも出力できます。未知のキーはエラーになるのでタイプミスを検出できます。ファイルの検査は wpg template lint FILE です。

schema: 1                  # 必須
meta: { ... }              # 必須
variables: [ ... ]         # 任意
layers: [ ... ]            # 必須。外側から内側の順
packet: { ... }            # 任意
stream: { ... }            # 任意
tx: { ... }                # 任意

meta

キー意味
name必須の表示名。ライブラリ内で一意 (同梱ライブラリはテストで確認)
description自由記述
descriptions言語別の説明: ja: と en:
tags検索用タグ。同梱ライブラリは protocol:<id>、layer:l2|l3|l4、scenario、malformed と話題語を使う
author, created自由記述。created は日付または RFC 3339 の時刻

variables

テンプレート引数です。各項目は name、任意の default (文字列・数値・真偽値)、description を持ちます。フィールド値やジェネレータの中で ${name} と書き、実行時に --var name=value (wpg send、wpg render) や GUI で上書きします。既定値も上書きも無い変数はエラーで、宣言の無い ${…} もエラーです。宣言だけで使われていない変数は警告になります。

次の任意のキーで値を制約できます。書かなければどんな文字列も受け付けます。

キー意味
typeuint、int、bool、string、ipv4、ipv6、mac、bytes。値の書式はフィールドと同じです (0x0800、0200.0000.0001)
choices許す値の一覧
min、max範囲 (両端を含む)。uint と int のみ
required実行時に値の指定が必須。default は持てません
variables:
  - { name: vlan_id, type: uint, min: 1, max: 4094, default: 100 }
  - { name: target, type: ipv4, required: true }
  - { name: proto, type: uint, choices: [6, 17], default: 17 }

wpg template lint は既定値と選択肢を検査します。実行時の値 (--var、ジョブ API と render API の vars) が合わなければテンプレートエラーになり、変数名と期待する値を示します。CLI は終了コード 3、サーバは 422 invalid_template を返します。bool は true、false、1、0 を受け付け、1 か 0 として代入されます。GUI のジョブ作成フォームは変数ごとに合った入力欄を表示します。

layers

各項目は protocol (カタログの ID。プロトコルリファレンス を参照)、fields、generators から成ります。

fields

書いたフィールドは Manual、書かない (または auto にした) フィールドは既定値になるか、計算されます (長さ、チェックサム、次プロトコル番号)。値は人間向け表記です。

型例
整数64、0x0800、0b101
MAC / IPv4 / IPv602:00:00:00:00:01、192.0.2.1、2001:db8::1 ("00:11:22:33:44:55" のように数字だけの MAC は YAML が数値と解釈しうるので引用符で囲む)
バイト列"00 01 02 03" (必ず引用符)。テキストは '"text"' (\r、\n、\t のエスケープ可)
テキスト行 (CRLF で終わる SIP の行)start_line: '"INVITE sip:bob@example.com SIP/2.0"' (終端は自動で付く)
enum数値かバリアント名: ethertype: arp、oper: request、type: echo_request
flagsビット名を | で連結: flags: syn|ack、flags: df
TLV リスト (TCP/IPv4 オプション)- mss: 1460、- sack_permitted、- nop、- timestamps: { tsval: 1, tsecr: 0 }、- { type: 99, data: "01 02" }
リスト / TLV リストをそのままのオクテット列でnlri: "18 c6 33 64" (リストの代わりに引用符付きの 16 進文字列。インポートがデコードできない内容をこの形で書く)

わざと誤った値も受け付けます。Manual の長さやチェックサムは書いたとおりに使われ、wpg template lint は不一致を警告 (diag-*) として報告します。templates/malformed/ のサンプルはこの仕組みで作っています。

generators

フィールド名からジェネレータへの対応です。完全形か、GUI と同じ短縮表記で書きます。

kindパラメータ短縮表記意味
rangestart、end、step (既定 1)192.168.1.1-192.168.5.254、1-1024両端を含むスイープ。件数が多いと先頭に戻る
range (CIDR)10.0.0.0/22ブロックのホストアドレス
listvalues1,5,9値を順に繰り返す
randommin、max、seed一様乱数 (整数、IPv4、IPv6)
random_macoui、seed00:11:22:*ランダムな MAC (上位を固定可)
counterstart、step、wrap増加する値。wrap が無ければ無限
filepathファイルから読む値
exprexpr計算式による値

同じフィールドに固定値とジェネレータを書くと lint が警告します。

式 (expr)

expr ジェネレータはパケットごとに整数を計算します。結果は他の値と同じようにフィールドに書き込まれるので、数値のほか IPv4 / IPv6 アドレスや MAC アドレスも埋められます。

要素意味
12、0x1f、0b101リテラル (10 進、16 進、2 進)
idxストリーム内のパケット番号 (0、1、2、…)
$field、$<layer>.field同じパケットでの、別の ジェネレータ付き フィールドの値。<layer> の書き方は stream.order と同じ ($0.src と $ethernet2.src はどちらも先頭レイヤの src ジェネレータ、$ipv4[1].src は 2 つ目の ipv4)
+ - * / %、<< >>、& ^ |、単項 - ~C と同じ演算子と優先順位。整数演算 (128 ビット、桁あふれは折り返し)
hash(x)、crc(x)x (8 オクテットのビッグエンディアン) の FNV-1a 64 / CRC-32。連番を散らすのに使う
rand(min, max)min から max までの乱数。パケット番号と stream.seed で決まる
min(a, ...)、max(a, ...)引数の最小 / 最大

参照できるのは、同じテンプレートでジェネレータを持つフィールドだけです。固定値のフィールドは見えません (変数にして ${name} と書けば、式の解析より前に置き換わります)。expr が別の expr を参照できるのは、参照先がテンプレート内で先にある場合だけです。同梱ライブラリの例:

generators:
  chaddr: { kind: expr, expr: "$0.src" }                          # Ethernet の送信元 MAC を写す (dhcp-discover-flood)
  xid: { kind: expr, expr: "rand(0, 0xffffffff)" }               # 再現可能なランダムのトランザクション ID
  label: { kind: expr, expr: "${srgb_base} + ${first_index} + idx" } # SR-MPLS の Prefix SID (sr-mpls-prefix-sid-sweep)

packet

キー既定意味
pad_to_mintrueEthernet の最小 60 オクテット (FCS 除く) までゼロ埋め
fcsnoneauto は正しい CRC-32、bad は誤った CRC-32 を付け、none は付けない

stream

ジェネレータの組み合わせ方とストリームの長さです。

キー意味
modezip (既定。同時に進める)、product (最後が最速)、nested
ordernested 用。遅い方から速い方へ <レイヤ>.<フィールド> で列挙。<レイヤ> は 0 始まりの添字、スタック内で一意なプロトコル ID (dot1q.vid、arp.tpa)、または同じプロトコルが複数あるときの <プロトコル>[<n>] (0 始まりで n 番目。ipv4[1].src はトンネル内側の IPv4)
countパケット数。既定は tx.count、無ければジェネレータの自然な長さ
seedランダムジェネレータの seed。ストリームを再現可能にする

無限のジェネレータ (wrap 無しの counter) には count が必要で、product や nested の内側には置けません。

tx

キー意味
count送信パケット数 (count も duration_secs も無ければ停止するまで)
duration_secs秒単位の時間制限
rate1000pps、1Mpps、10Gbps、50%、ifg=10us
burst連続送信するフレーム数

wpg send のコマンドラインオプションが優先されます。

コマンドラインからの上書き

wpg send、wpg render、wpg template show、wpg bench --template では、ファイルを編集せずにその実行だけテンプレートを変えられます。

オプション効果
--vars-file FILEYAML または JSON のマップ (dst_ip: 10.1.2.3) から変数の値を読みます。
--var NAME=VALUE変数を 1 つ指定します (複数回指定可)。--vars-file より優先され、--vars-file はテンプレートの default より優先されます。
--set LAYER.FIELD=VALUEフィールドを fields: と同じ表記の値に固定します。フィールドは Manual になり、ジェネレータは外れます。
--gen LAYER.FIELD=GENフィールドにジェネレータを付け、固定値を外します。

LAYER はプロトコル ID (最初のレイヤ: ipv4)、0 から数えた n 番目のレイヤ (ipv4[1] は IP-in-IP の内側のヘッダ)、または 0 始まりのレイヤ添字 (2) です。FIELD は プロトコルリファレンス にあるフィールド名です。存在しないレイヤやフィールドを指定すると終了コード 3 で止まり、テンプレートのレイヤ一覧か近いフィールド名を表示します。--vars-file の変数も --var と同じく、テンプレートで宣言されている必要があります。

GEN は次のいずれかです。

短縮表記ジェネレータ
range:A..B、range:A..B/STEPA から B まで (両端を含む) の range
list:a,b,clist
random、random:MIN..MAXrandom
counter、counter:START、counter:START/STEPcounter
expr:EXPRexpr
上記以外generators: の短縮表記: 10.0.0.1-10.0.0.9、10.0.0.0/24、1,5,9、00:11:22:*
$ wpg render templates/examples/eth-ipv4-udp-range.wpg.yaml -o out.pcapng --count 100 \
    --vars-file lab.yaml --set ipv4.ttl=1 --gen udp.src_port=range:1024..1123

payload レイヤ

payload は実際のプロトコルではなく、フィールドが生成を制御します。mode (fixed、pattern、random、counter)、data、length、seed、counter モードでは counter_offset、counter_width (u32 または u64)、counter_value (通常は counter ジェネレータで与える) です。raw は指定したバイト列をそのまま置きます。

lint の検査項目

キー重大度意味
lint-schema-versionerror未対応の schema
lint-missing-namewarningmeta.name が空
lint-no-layerserrorレイヤなし
lint-unknown-protocol, lint-unknown-fielderrorカタログにない
lint-invalid-value, lint-invalid-generatorerror値やジェネレータがフィールド型に合わない
lint-unknown-variable, lint-missing-variableerror宣言のない変数 / 値のない変数
lint-unused-variablewarning宣言だけで未使用
lint-next-protocol-manualwarning次レイヤが前のレイヤのバインディング表になく、次プロトコルのフィールドを Auto にできない
lint-field-and-generatorwarning同じフィールドに固定値とジェネレータ
diag-*error / warning構築したパケットの診断 (計算値と異なるチェックサムや長さなど)

同梱ライブラリ

templates/ は既定でライブラリとして読み込まれ、crates/wpg-template/tests/samples.rs で検証されています。

フォルダ内容
examples/小さな出発点 5 本
protocols/l2, l3, l4, payload, frameカタログの全プロトコルのサンプル。ジャンボ、最小サイズ、ランツ、FCS の例を含む
scenarios/すぐ使える試験: ARP スイープ、SYN フラッドとポートスキャン、QinQ、NDP と RA、ping と TTL スイープ、QoS マトリクス、MTU 探索、フレーム長スイープ、MAC フラッド、SR-MPLS Prefix SID スイープ、DHCP DISCOVER 負荷
interop/実機・FRR との相互接続確認: OSPFv2 2-Way、RIPv2 経路、IS-IS 隣接 Up、VRRPv3 プリエンプト、PIM ネイバー、LDP ディスカバリ、BFD セッション Up (interop タグ。手順は運用ガイドの「相互接続確認」)
malformed/わざと壊したパケット (チェックサム不正、長さ不一致、切り詰め、FCS 不正)。設計上 lint が警告を出し、malformed タグが付く

一覧は wpg template list templates で表示できます。

キーリファレンス (自動生成)

テンプレート形式の JSON Schema (wpg template schema) から自動生成しています。説明文はコードの doc コメント (英語) です。Template が文書のルートです。

Template

A .wpg.yaml template: one packet (+ generators) = one stream.

キー型必須説明
layersTemplateLayer[]Layer stack, outermost first.
metaMetaはいMetadata.
packetPacketOptionsFrame-level options (padding, FCS).
schemaintegerはいSchema version, see [SCHEMA_VERSION].
streamStreamOptionsStream expansion options.
txTxProfileTransmit profile.
variablesVariable[]Template variables.

FcsChoice

Frame check sequence choice.

値説明
"auto"Append the correct CRC-32.
"bad"Append a deliberately wrong CRC-32.
"none"No FCS (what capture / TX backends usually expect).

FieldInput

A field value as written in YAML / JSON: a scalar in human notation or a structured value for TLV (options) and list fields.

  • scalars: 64, "192.168.1.1", 0x0800, syn|ack (flag names), ipv4 (enum variant names), auto. Hex byte strings must be quoted ("00 01 02");
  • TLV: a list of items, each a bare variant name (nop), a one-key map ({ mss: 1460 }, { timestamps: { tsval: 1, tsecr: 0 } }) or the explicit form { type: 99, data: "01 02" } / { name: mss, length: 4, fields: { value: 1 } };
  • list: a list of scalars, or of maps for lists of field groups.
値説明
nullEmpty (~): e.g. an option without value.
booleantrue / false (1 / 0).
integerUnsigned integer.
integerNegative integer.
stringText in human notation.
FieldInput[]Sequence (TLV items, list elements).
map<string, FieldInput>Mapping (TLV item, field group).

Generator

Field value generator, tagged by kind.

kind: "range"

キー型必須説明
endstring | integerはい
kind"range"はい
startstring | integerはい
stepinteger

kind: "list"

キー型必須説明
kind"list"はい
values(string | integer)[]はい

kind: "random"

キー型必須説明
kind"random"はい
maxstring | integer
minstring | integer
seedinteger

kind: "random_mac"

キー型必須説明
kind"random_mac"はい
ouistring
seedinteger

kind: "counter"

キー型必須説明
kind"counter"はい
startinteger
stepinteger
wrapinteger

kind: "file"

キー型必須説明
kind"file"はい
pathstringはい

kind: "expr"

キー型必須説明
exprstringはい
kind"expr"はい

GeneratorSpec

A generator of a layer field: either a full [Generator] or the human shorthand of the GUI (192.168.1.1-192.168.5.254, 10.0.0.0/24, 1,5,9, 00:11:22:*).

値説明
GeneratorFull form, e.g. { kind: range, start: 192.168.1.1, end: 192.168.1.254 }.
stringShorthand parsed with wpg_core::notation::parse_generator.

Meta

Template metadata.

キー型必須説明
authorstring | nullAuthor.
createdstring | nullCreation timestamp (RFC 3339 or a plain date).
descriptionstring | nullFree-form description.
descriptionsmap<string, ``string``>Localised descriptions keyed by language tag (ja, en).
namestringはいDisplay name.
tagsstring``[]Tags for search / grouping.

PacketOptions

Frame-level packet options.

キー型必須説明
fcsFcsChoiceFrame check sequence.
pad_to_minbooleanZero-pad to the minimum frame length (Ethernet: 60 octets).

Scalar

A YAML scalar; variable defaults may be written as numbers.

値説明
string
integer
integer
boolean

StreamMode

How generators of several fields are combined (see wpg_core::stream::Mode).

値説明
"zip"Advance all generators in lockstep.
"product"Cartesian product, the last generator varies fastest.
"nested"Product with an explicit priority given by [StreamOptions::order].

StreamOptions

Stream expansion options.

キー型必須説明
countinteger | nullNumber of packets; defaults to tx.count, then to the natural length.
modeStreamModeCombination mode of the generators.
orderstring``[]For nested: fields from the slowest to the fastest, as <layer>.<field> where <layer> is a 0-based index, a protocol id (ipv4.src) or the n-th occurrence of a protocol (ipv4[1].src).
seedinteger | nullSeed for random generators.

TemplateLayer

One layer of the template’s packet.

キー型必須説明
fieldsmap<string, FieldInput>Manually set fields in human notation (192.168.1.1, 0x0800, syn|ack, ${src_ip}). Unlisted fields use their default or Auto value; the literal auto also leaves a field Auto. TLV / list fields take structured values.
generatorsmap<string, GeneratorSpec>Generators varying fields across the stream.
protocolstringはいProtocol id from the catalog (e.g. ethernet2).

TxProfile

How the stream is transmitted.

キー型必須説明
burstinteger | nullBurst size in packets.
countinteger | nullNumber of packets (None together with duration_secs: None = infinite).
duration_secsnumber | nullDuration limit in seconds.
ratestring | nullRate in human notation: 1000pps, 1Mpps, 10Gbps, 50%.

VarType

Value type of a template variable (the field-type names of the protocol catalog).

値説明
"uint"Unsigned integer: 64, 0x0800, 0b101.
"int"Signed integer: -1, +5, 0x10.
"bool"true / false (or 1 / 0); substituted as 1 / 0.
"string"Any text.
"ipv4"IPv4 address.
"ipv6"IPv6 address.
"mac"MAC address.
"bytes"Octets in hex: "00 01 02".

Variable

A template argument (${name} placeholder) overridable at run time.

キー型必須説明
choicesScalar[]Allowed values; any other value is rejected.
defaultScalar | nullDefault value (human notation; numbers and booleans are accepted too).
descriptionstring | nullDescription shown in the GUI / CLI.
maxinteger | nullLargest allowed value (type: uint / int only).
mininteger | nullSmallest allowed value (type: uint / int only).
namestringはいPlaceholder name.
requiredbooleanA value must be given at run time; the variable has no default.
typeVarType | nullValue type; values (default, --var, job vars) are checked against it. Without a type any text is accepted.