Declarative Diagramming (D2) Output Module

d2 output module create a description of network topology in D2 diagram scripting language. You can use that description with D2 commands to create topology diagrams.

The d2 output module is invoked by specifying the -o d2 parameter in the netlab create command or by specifying the d2 engine in the netlab graph command. It takes an optional destination file name (default: graph.d2).

A single formatting modifier can be used to specify the graph type:

  • topology (default) – Includes point-to-point links, multi-access bridges, and stub subnets. When the network topology contains BGP information, the graph groups nodes into autonomous systems. Alternatively, you could set defaults.outputs.graph.groups attribute to use topology groups to group graph nodes.

  • bgp – Include autonomous systems, nodes, and BGP sessions. With the rr option (specified with netlab create -o graph:bgp:rr), RR-client sessions are drawn as directed arrows.

Tip

The network topology graph description contains nodes and links, but no placement information. D2 is pretty good at figuring out how to draw the required graph, but it pays off to test out the layout engines. Changing the order of links might also unclutter the diagrams.

Modifying Graph Attributes

Graphing routines use default topology settings to modify the node- or link parameters of the generated D2 file:

  • outputs.d2.interface_labels (default: False) – Add IP addresses to links in topology graph. Results in a cluttered image.

  • outputs.d2.groups (default: None) – use the specified list of groups (or all groups when set to True) to create graph clusters

  • outputs.d2.node_address_label (default: True) – add node loopback IP addresses or IP addresses of the first interface (for hosts) to node labels.

  • outputs.d2.node_interfaces (default: False) – add list of interfaces and their IP addresses to nodes[1].

  • outputs.d2.as_clusters (default: True) – use BGP autonomous systems to cluster nodes in the topology graph. BGP AS clusters are always used in BGP graphs.

  • outputs.d2.rr_sessions (default: True) – draw IBGP sessions between BGP route reflectors and clients as directional connections.

Modifying Shape and Connection Attributes

d2 output module uses graphite.icon device attribute to select the node style defined in defaults.outputs.d2 settings.

You can also style lan shapes, container objects (groups or autonomous systems), and ibgp or ebgp sessions.

You can use any D2 style attribute in these default settings. The following printout lists the system defaults within defaults.output.d2 dictionary:

router:
  shape: oval

switch:
  shape: hexagon

lan:
  shape: rectangle
  style.border-radius: 8

ibgp:
  style:
    stroke: '#613913'
    stroke-width: 4
  source-arrowhead:
    shape: arrow
  target-arrowhead:
    shape: arrow

ebgp:
  style:
    stroke: '#b21a1a'
    stroke-width: 4
  source-arrowhead:
    shape: arrow
  target-arrowhead:
    shape: arrow

Specifying D2 Attributes

You could specify D2 attributes in your topology file (where you would have to prefix them with defaults), in per-user topology defaults, or with environment variables (even more details). You could also specify them with the -s parameter of the netlab create command, yet again prefixed with defaults (more details).

Use the netlab show defaults outputs.d2 command to show the current D2 defaults, including topology file defaults and user defaults, for example:

% netlab show defaults outputs.d2

netlab default settings within the outputs.d2 subtree
=====================================================

as_clusters: true
ebgp:
  source-arrowhead:
    shape: arrow
  style:
    stroke: '#b21a1a'
    stroke-width: 4
  target-arrowhead:
    shape: arrow
ibgp:
  source-arrowhead:
    shape: arrow
  style:
    stroke: '#613913'
    stroke-width: 4
  target-arrowhead:
    shape: arrow
interface_labels: false
lan:
  shape: rectangle
  style:
    border-radius: 8
    font-size: 20
node_address_label: true
router:
  shape: oval
  style:
    font-size: 20
rr_sessions: true
switch:
  shape: hexagon
  style:
    font-size: 20

Extending D2 Style Attributes

You can define your own link/node style attributes:

  • Define the attribute within defaults.outputs.d2.attributes dictionary. For example, you might define d2.background node attribute (a string) to add background color to nodes[2]:

defaults.outputs.d2.attributes.node.background: str
  • Define a mapping between your attribute and D2 style attribute within the defaults.outputs.d2.styles dictionary. For example, your d2.background attribute maps into D2 style.fill attribute:

defaults.outputs.d2.styles.background: fill