xen-orchestra/@xen-orchestra/openflow
2023-07-28 10:05:26 +02:00
..
src chore: format with Prettier 2021-06-03 14:49:14 +02:00
.babelrc.js chore: enforce strict mode for CJS files 2022-02-22 12:34:41 +01:00
.eslintrc.js chore(eslint): only use @babel/eslint-parser for pkgs using Babel 2021-04-23 14:47:34 +02:00
.npmignore feat: unified .npmignore for all packages 2021-04-07 13:58:14 +02:00
.USAGE.md chore: hide USAGE.md 2022-02-18 17:11:52 +01:00
package.json feat: technical release (#6956) 2023-07-28 10:05:26 +02:00
parse-socket.js chore: enforce strict mode for CJS files 2022-02-22 12:34:41 +01:00
README.md docs: uniformize code blocks 2023-02-06 11:25:12 +01:00

@xen-orchestra/openflow

Package Version License PackagePhobia Node compatibility

Pack and unpack OpenFlow messages

Install

Installation of the npm package:

npm install --save @xen-orchestra/openflow

Usage

Unpacking a received OpenFlow message from a socket:

import openflow from '@xen-orchestra/openflow'
import parse from '@xen-orchestra/openflow/parse-socket'

const version = openflow.versions.openFlow11
const ofProtocol = openflow.protocols[version]

function parseOpenFlowMessages(socket) {
  for await (const msg of parse(socket)) {
    if (msg.header !== undefined) {
      const ofType = msg.header.type
      switch (ofType) {
        case ofProtocol.type.hello:
          // Handle OFPT_HELLO
          break
        case ofProtocol.type.error:
          // Handle OFPT_ERROR
          break
        case ofProtocol.type.echoRequest:
          // Handle OFPT_ECHO_REQUEST
          break
        case ofProtocol.type.packetIn:
          // Handle OFPT_PACKET_IN
          break
        case ofProtocol.type.featuresReply:
          // Handle OFPT_FEATURES_REPLY
          break
        case ofProtocol.type.getConfigReply:
          // Handle OFPT_GET_CONFIG_REPLY
          break
        case ofProtocol.type.portStatus:
          // Handle OFPT_PORT_STATUS
          break
        case ofProtocol.type.flowRemoved:
          // Handle OFPT_FLOW_REMOVED
          break
        default:
          // Error: Invalid type
          break
      }
    } else {
      // Error: Message is unparseable
    }
  }
}

Unpacking a OpenFlow message from a buffer:

import openflow from '@xen-orchestra/openflow'

const version = openflow.versions.openFlow11
const ofProtocol = openflow.protocols[version]

function processOpenFlowMessage(buf) {
  const unpacked = openflow.unpack(buf)
  const ofType = unpacked.header.type
  switch (ofType) {
    case ofProtocol.type.hello:
      // Handle OFPT_HELLO
      break
    case ofProtocol.type.error:
      // Handle OFPT_ERROR
      break
    case ofProtocol.type.echoRequest:
      // Handle OFPT_ECHO_REQUEST
      break
    case ofProtocol.type.packetIn:
      // Handle OFPT_PACKET_IN
      break
    case ofProtocol.type.featuresReply:
      // Handle OFPT_FEATURES_REPLY
      break
    case ofProtocol.type.getConfigReply:
      // Handle OFPT_GET_CONFIG_REPLY
      break
    case ofProtocol.type.portStatus:
      // Handle OFPT_PORT_STATUS
      break
    case ofProtocol.type.flowRemoved:
      // Handle OFPT_FLOW_REMOVED
      break
    default:
      // Error: Invalid type
      break
  }
}

Packing an OpenFlow OFPT_HELLO message:

import openflow from '@xen-orchestra/openflow'

const version = openflow.versions.openFlow11
const ofProtocol = openflow.protocols[version]

const buf = openflow.pack({
  header: {
    version,
    type: ofProtocol.type.hello,
    xid: 1,
  },
})

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Vates SAS