summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Luong <sluong@cisco.com>2021-06-17 08:50:32 -0700
committerDamjan Marion <dmarion@me.com>2021-07-01 12:55:29 +0000
commita57a7005d6c04029d168cfdd053f3e334b935cc3 (patch)
tree2760cef8fe3ed355e8ab995364e4c336c07ea67d
parent5ff59a1f8b7f6de7738a0fd95e1dbf250dbf1a6a (diff)
vmxnet3: support manual thread assignment to tx queue
Thread assignment to tx queue has always been automatic and there was no way to modify it. With this patch, it is now possible to use the cli "set interface tx-queue" to change the thread assignment to tx queue for vmxnet3 interface, thanks to the new tx infra. Type: feature Signed-off-by: Steven Luong <sluong@cisco.com> Change-Id: I1544e3557f70251d4bd423cc3d9f28ee1d44db4a
-rw-r--r--src/plugins/vmxnet3/output.c9
-rw-r--r--src/plugins/vmxnet3/vmxnet3.c34
-rw-r--r--src/plugins/vmxnet3/vmxnet3.h1
3 files changed, 26 insertions, 18 deletions
diff --git a/src/plugins/vmxnet3/output.c b/src/plugins/vmxnet3/output.c
index 0de91d9bf21..8ba3f99022f 100644
--- a/src/plugins/vmxnet3/output.c
+++ b/src/plugins/vmxnet3/output.c
@@ -110,7 +110,8 @@ VNET_DEVICE_CLASS_TX_FN (vmxnet3_device_class) (vlib_main_t * vm,
u16 space_left;
u16 n_left = frame->n_vectors;
vmxnet3_txq_t *txq;
- u16 qid = vm->thread_index % vd->num_tx_queues, produce;
+ vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame);
+ u16 qid = tf->queue_id, produce;
if (PREDICT_FALSE (!(vd->flags & VMXNET3_DEVICE_F_LINK_UP)))
{
@@ -121,7 +122,8 @@ VNET_DEVICE_CLASS_TX_FN (vmxnet3_device_class) (vlib_main_t * vm,
}
txq = vec_elt_at_index (vd->txqs, qid);
- clib_spinlock_lock_if_init (&txq->lock);
+ if (tf->shared_queue)
+ clib_spinlock_lock (&txq->lock);
vmxnet3_txq_release (vm, vd, txq);
@@ -228,7 +230,8 @@ VNET_DEVICE_CLASS_TX_FN (vmxnet3_device_class) (vlib_main_t * vm,
if (PREDICT_TRUE (produce != txq->tx_ring.produce))
vmxnet3_reg_write_inline (vd, 0, txq->reg_txprod, txq->tx_ring.produce);
- clib_spinlock_unlock_if_init (&txq->lock);
+ if (tf->shared_queue)
+ clib_spinlock_unlock (&txq->lock);
return (frame->n_vectors - n_left);
}
diff --git a/src/plugins/vmxnet3/vmxnet3.c b/src/plugins/vmxnet3/vmxnet3.c
index aeb2af36d0a..ff0a7dc706b 100644
--- a/src/plugins/vmxnet3/vmxnet3.c
+++ b/src/plugins/vmxnet3/vmxnet3.c
@@ -20,6 +20,7 @@
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <vnet/interface/rx_queue_funcs.h>
+#include <vnet/interface/tx_queue_funcs.h>
#include <vmxnet3/vmxnet3.h>
#define PCI_VENDOR_ID_VMWARE 0x15ad
@@ -325,23 +326,15 @@ vmxnet3_txq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
vmxnet3_tx_stats *txs;
u32 size;
- if (qid >= vd->num_tx_queues)
- {
- qid = qid % vd->num_tx_queues;
- txq = vec_elt_at_index (vd->txqs, qid);
- if (txq->lock == 0)
- clib_spinlock_init (&txq->lock);
- vd->flags |= VMXNET3_DEVICE_F_SHARED_TXQ_LOCK;
- return 0;
- }
+ vec_validate_aligned (vd->txqs, qid, CLIB_CACHE_LINE_BYTES);
+ txq = vec_elt_at_index (vd->txqs, qid);
+ clib_memset (txq, 0, sizeof (*txq));
+ clib_spinlock_init (&txq->lock);
vec_validate (vd->tx_stats, qid);
txs = vec_elt_at_index (vd->tx_stats, qid);
clib_memset (txs, 0, sizeof (*txs));
- vec_validate_aligned (vd->txqs, qid, CLIB_CACHE_LINE_BYTES);
- txq = vec_elt_at_index (vd->txqs, qid);
- clib_memset (txq, 0, sizeof (*txq));
txq->size = qsz;
txq->reg_txprod = qid * 8 + VMXNET3_REG_TXPROD;
@@ -351,7 +344,7 @@ vmxnet3_txq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
if (txq->tx_desc == 0)
return vlib_physmem_last_error (vm);
- memset (txq->tx_desc, 0, size);
+ clib_memset (txq->tx_desc, 0, size);
size = qsz * sizeof (*txq->tx_comp);
txq->tx_comp =
@@ -407,7 +400,6 @@ vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
{
clib_error_t *error = 0;
u32 ret, i, size;
- vlib_thread_main_t *tm = vlib_get_thread_main ();
/* Quiesce the device */
vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
@@ -506,7 +498,7 @@ vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
return error;
}
- for (i = 0; i < tm->n_vlib_mains; i++)
+ for (i = 0; i < vd->num_tx_queues; i++)
{
error = vmxnet3_txq_init (vm, vd, i, args->txq_size);
if (error)
@@ -834,6 +826,18 @@ vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args)
vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
}
+
+ vec_foreach_index (qid, vd->txqs)
+ {
+ vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
+ txq->queue_index =
+ vnet_hw_if_register_tx_queue (vnm, vd->hw_if_index, qid);
+ }
+ for (u32 i = 0; i < vlib_get_n_threads (); i++)
+ {
+ u32 qi = vd->txqs[i % vd->num_tx_queues].queue_index;
+ vnet_hw_if_tx_queue_assign_thread (vnm, qi, i);
+ }
vnet_hw_if_update_runtime_data (vnm, vd->hw_if_index);
vd->flags |= VMXNET3_DEVICE_F_INITIALIZED;
diff --git a/src/plugins/vmxnet3/vmxnet3.h b/src/plugins/vmxnet3/vmxnet3.h
index e8d2be0e552..75107689443 100644
--- a/src/plugins/vmxnet3/vmxnet3.h
+++ b/src/plugins/vmxnet3/vmxnet3.h
@@ -543,6 +543,7 @@ typedef struct
{
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
u16 size;
+ u32 queue_index;
u32 reg_txprod;
clib_spinlock_t lock;
2 } /* Name */ .highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * ip/ip4_pg: IP v4 packet-generator interface
 *
 * Copyright (c) 2008 Eliot Dresselhaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <vnet/ip/ip.h>
#include <vnet/pg/pg.h>

#define IP4_PG_EDIT_CHECKSUM (1 << 0)
#define IP4_PG_EDIT_LENGTH (1 << 1)

static_always_inline void
compute_length_and_or_checksum (vlib_main_t * vm,
				u32 * packets,
				u32 n_packets,
				u32 ip_header_offset, u32 flags)
{
  ASSERT (flags != 0);

  while (n_packets >= 2)
    {
      u32 pi0, pi1;
      vlib_buffer_t *p0, *p1;
      ip4_header_t *ip0, *ip1;
      ip_csum_t sum0, sum1;

      pi0 = packets[0];
      pi1 = packets[1];
      p0 = vlib_get_buffer (vm, pi0);
      p1 = vlib_get_buffer (vm, pi1);
      n_packets -= 2;
      packets += 2;

      ip0 = (void *) (p0->data + ip_header_offset);
      ip1 = (void *) (p1->data + ip_header_offset);

      if (flags & IP4_PG_EDIT_LENGTH)
	{
	  ip0->length =
	    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) -
				  ip_header_offset);
	  ip1->length =
	    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p1) -
				  ip_header_offset);
	}

      if (flags & IP4_PG_EDIT_CHECKSUM)
	{
	  ASSERT (ip4_header_bytes (ip0) == sizeof (ip0[0]));
	  ASSERT (ip4_header_bytes (ip1) == sizeof (ip1[0]));

	  ip0->checksum = 0;
	  ip1->checksum = 0;

	  ip4_partial_header_checksum_x2 (ip0, ip1, sum0, sum1);
	  ip0->checksum = ~ip_csum_fold (sum0);
	  ip1->checksum = ~ip_csum_fold (sum1);

	  ASSERT (ip0->checksum == ip4_header_checksum (ip0));
	  ASSERT (ip1->checksum == ip4_header_checksum (ip1));
	}
    }

  while (n_packets >= 1)
    {
      u32 pi0;
      vlib_buffer_t *p0;
      ip4_header_t *ip0;
      ip_csum_t sum0;

      pi0 = packets[0];
      p0 = vlib_get_buffer (vm, pi0);
      n_packets -= 1;
      packets += 1;

      ip0 = (void *) (p0->data + ip_header_offset);

      if (flags & IP4_PG_EDIT_LENGTH)
	ip0->length =
	  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) -
				ip_header_offset);

      if (flags & IP4_PG_EDIT_CHECKSUM)
	{
	  ASSERT (ip4_header_bytes (ip0) == sizeof (ip0[0]));

	  ip0->checksum = 0;

	  ip4_partial_header_checksum_x1 (ip0, sum0);
	  ip0->checksum = ~ip_csum_fold (sum0);

	  ASSERT (ip0->checksum == ip4_header_checksum (ip0));
	}
    }
}

static void
ip4_pg_edit_function (pg_main_t * pg,
		      pg_stream_t * s,
		      pg_edit_group_t * g, u32 * packets, u32 n_packets)
{
  vlib_main_t *vm = vlib_get_main ();
  u32 ip_offset;

  ip_offset = g->start_byte_offset;

  switch (g->edit_function_opaque)
    {
    case IP4_PG_EDIT_LENGTH:
      compute_length_and_or_checksum (vm, packets, n_packets, ip_offset,
				      IP4_PG_EDIT_LENGTH);
      break;

    case IP4_PG_EDIT_CHECKSUM:
      compute_length_and_or_checksum (vm, packets, n_packets, ip_offset,
				      IP4_PG_EDIT_CHECKSUM);
      break;

    case IP4_PG_EDIT_LENGTH | IP4_PG_EDIT_CHECKSUM:
      compute_length_and_or_checksum (vm, packets, n_packets, ip_offset,
				      IP4_PG_EDIT_LENGTH
				      | IP4_PG_EDIT_CHECKSUM);
      break;

    default:
      ASSERT (0);
      break;
    }
}

typedef struct
{
  pg_edit_t ip_version, header_length;
  pg_edit_t tos;
  pg_edit_t length;

  pg_edit_t fragment_id, fragment_offset;

  /* Flags together with fragment offset. */
  pg_edit_t mf_flag, df_flag, ce_flag;

  pg_edit_t ttl;

  pg_edit_t protocol;

  pg_edit_t checksum;

  pg_edit_t src_address, dst_address;
} pg_ip4_header_t;

static inline void
pg_ip4_header_init (pg_ip4_header_t * p)
{
  /* Initialize fields that are not bit fields in the IP header. */
#define _(f) pg_edit_init (&p->f, ip4_header_t, f);
  _(tos);
  _(length);
  _(fragment_id);
  _(ttl);
  _(protocol);
  _(checksum);
  _(src_address);
  _(dst_address);
#undef _

  /* Initialize bit fields. */
  pg_edit_init_bitfield (&p->header_length, ip4_header_t,
			 ip_version_and_header_length, 0, 4);
  pg_edit_init_bitfield (&p->ip_version, ip4_header_t,
			 ip_version_and_header_length, 4, 4);

  pg_edit_init_bitfield (&p->fragment_offset, ip4_header_t,
			 flags_and_fragment_offset, 0, 13);
  pg_edit_init_bitfield (&p->mf_flag, ip4_header_t,
			 flags_and_fragment_offset, 13, 1);
  pg_edit_init_bitfield (&p->df_flag, ip4_header_t,
			 flags_and_fragment_offset, 14, 1);
  pg_edit_init_bitfield (&p->ce_flag, ip4_header_t,
			 flags_and_fragment_offset, 15, 1);
}

uword
unformat_pg_ip4_header (unformat_input_t * input, va_list * args)
{
  pg_stream_t *s = va_arg (*args, pg_stream_t *);
  pg_ip4_header_t *p;
  u32 group_index;

  p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ip4_header_t),
			    &group_index);
  pg_ip4_header_init (p);

  /* Defaults. */
  pg_edit_set_fixed (&p->ip_version, 4);
  pg_edit_set_fixed (&p->header_length, sizeof (ip4_header_t) / sizeof (u32));

  pg_edit_set_fixed (&p->tos, 0);
  pg_edit_set_fixed (&p->ttl, 64);

  pg_edit_set_fixed (&p->fragment_id, 0);
  pg_edit_set_fixed (&p->fragment_offset, 0);
  pg_edit_set_fixed (&p->mf_flag, 0);
  pg_edit_set_fixed (&p->df_flag, 0);
  pg_edit_set_fixed (&p->ce_flag, 0);

  p->length.type = PG_EDIT_UNSPECIFIED;
  p->checksum.type = PG_EDIT_UNSPECIFIED;

  if (unformat (input, "%U: %U -> %U",
		unformat_pg_edit,
		unformat_ip_protocol, &p->protocol,
		unformat_pg_edit,
		unformat_ip4_address, &p->src_address,
		unformat_pg_edit, unformat_ip4_address, &p->dst_address))
    goto found;

  if (!unformat (input, "%U:",
		 unformat_pg_edit, unformat_ip_protocol, &p->protocol))
    goto error;

found:
  /* Parse options. */
  while (1)
    {
      if (unformat (input, "version %U",
		    unformat_pg_edit, unformat_pg_number, &p->ip_version))
	;

      else if (unformat (input, "header-length %U",
			 unformat_pg_edit,
			 unformat_pg_number, &p->header_length))
	;

      else if (unformat (input, "tos %U",
			 unformat_pg_edit, unformat_pg_number, &p->tos))
	;

      else if (unformat (input, "length %U",
			 unformat_pg_edit, unformat_pg_number, &p->length))
	;

      else if (unformat (input, "checksum %U",
			 unformat_pg_edit, unformat_pg_number, &p->checksum))
	;

      else if (unformat (input, "ttl %U",
			 unformat_pg_edit, unformat_pg_number, &p->ttl))
	;

      else if (unformat (input, "fragment id %U offset %U",
			 unformat_pg_edit,
			 unformat_pg_number, &p->fragment_id,
			 unformat_pg_edit,
			 unformat_pg_number, &p->fragment_offset))
	{
	  int i;
	  for (i = 0; i < ARRAY_LEN (p->fragment_offset.values); i++)
	    pg_edit_set_value (&p->fragment_offset, i,
			       pg_edit_get_value (&p->fragment_offset,
						  i) / 8);

	}

      /* Flags. */
      else if (unformat (input, "mf") || unformat (input, "MF"))
	pg_edit_set_fixed (&p->mf_flag, 1);

      else if (unformat (input, "df") || unformat (input, "DF"))
	pg_edit_set_fixed (&p->df_flag, 1);

      else if (unformat (input, "ce") || unformat (input, "CE"))
	pg_edit_set_fixed (&p->ce_flag, 1);

      /* Can't parse input: try next protocol level. */
      else
	break;
    }

  {
    ip_main_t *im = &ip_main;
    ip_protocol_t protocol;
    ip_protocol_info_t *pi;

    pi = 0;
    if (p->protocol.type == PG_EDIT_FIXED)
      {
	protocol = pg_edit_get_value (&p->protocol, PG_EDIT_LO);
	pi = ip_get_protocol_info (im, protocol);
      }

    if (pi && pi->unformat_pg_edit
	&& unformat_user (input, pi->unformat_pg_edit, s))
      ;

    else if (!unformat_user (input, unformat_pg_payload, s))
      goto error;

    if (p->length.type == PG_EDIT_UNSPECIFIED
	&& s->min_packet_bytes == s->max_packet_bytes
	&& group_index + 1 < vec_len (s->edit_groups))
      {
	pg_edit_set_fixed (&p->length,
			   pg_edit_group_n_bytes (s, group_index));
      }

    /* Compute IP header checksum if all edits are fixed. */
    if (p->checksum.type == PG_EDIT_UNSPECIFIED)
      {
	ip4_header_t fixed_header, fixed_mask, cmp_mask;

	/* See if header is all fixed and specified except for
	   checksum field. */
	memset (&cmp_mask, ~0, sizeof (cmp_mask));
	cmp_mask.checksum = 0;

	pg_edit_group_get_fixed_packet_data (s, group_index,
					     &fixed_header, &fixed_mask);
	if (!memcmp (&fixed_mask, &cmp_mask, sizeof (cmp_mask)))
	  pg_edit_set_fixed (&p->checksum,
			     clib_net_to_host_u16 (ip4_header_checksum
						   (&fixed_header)));
      }

    p = pg_get_edit_group (s, group_index);
    if (p->length.type == PG_EDIT_UNSPECIFIED
	|| p->checksum.type == PG_EDIT_UNSPECIFIED)
      {
	pg_edit_group_t *g = pg_stream_get_group (s, group_index);
	g->edit_function = ip4_pg_edit_function;
	g->edit_function_opaque = 0;
	if (p->length.type == PG_EDIT_UNSPECIFIED)
	  g->edit_function_opaque |= IP4_PG_EDIT_LENGTH;
	if (p->checksum.type == PG_EDIT_UNSPECIFIED)
	  g->edit_function_opaque |= IP4_PG_EDIT_CHECKSUM;
      }

    return 1;
  }

error:
  /* Free up any edits we may have added. */
  pg_free_edit_group (s);
  return 0;
}


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */