[dpdk-dev] [PATCH v1 6/7] ethdev: generate flow API conversion header

Adrien Mazarguil adrien.mazarguil at 6wind.com
Thu Oct 5 11:49:07 CEST 2017


Add script and build target to generate rte_flow_conv.h (used by
rte_flow_conv()) from rte_flow.h.

Although the resulting header file is internal and not public, this is not
done automatically since it is versioned in the source tree.

Developers can update it after making changes to rte_flow.h by running:

 make lib/librte_ether_sub RTE_MAKE_SUBTARGET=rte_flow_conv.h

Signed-off-by: Adrien Mazarguil <adrien.mazarguil at 6wind.com>
---
 MAINTAINERS                       |   1 +
 buildtools/gen-rte_flow_conv-h.sh | 264 +++++++++++++++++++++++++++++++++
 lib/librte_ether/Makefile         |  10 ++
 3 files changed, 275 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 9eec984..002b54e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -253,6 +253,7 @@ F: devtools/test-null.sh
 
 Flow API
 M: Adrien Mazarguil <adrien.mazarguil at 6wind.com>
+F: buildtools/gen-rte_flow_conv-h.sh
 F: lib/librte_ether/rte_flow*
 
 Traffic Management API - EXPERIMENTAL
diff --git a/buildtools/gen-rte_flow_conv-h.sh b/buildtools/gen-rte_flow_conv-h.sh
new file mode 100755
index 0000000..482e733
--- /dev/null
+++ b/buildtools/gen-rte_flow_conv-h.sh
@@ -0,0 +1,264 @@
+#!/bin/sh -e
+#
+#   BSD LICENSE
+#
+#   Copyright 2017 6WIND S.A.
+#
+#   Redistribution and use in source and binary forms, with or without
+#   modification, are permitted provided that the following conditions
+#   are met:
+#
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above copyright
+#       notice, this list of conditions and the following disclaimer in
+#       the documentation and/or other materials provided with the
+#       distribution.
+#     * Neither the name of 6WIND S.A. nor the names of its
+#       contributors may be used to endorse or promote products derived
+#       from this software without specific prior written permission.
+#
+#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# This script generates an internal flow API header file needed by
+# conversion function rte_flow_conv().
+#
+# This is done by feeding rte_flow.h to the C preprocessor and transforming
+# its output into a different C header file whose name is provided as a
+# command-line argument.
+#
+# CC, CPPFLAGS, CFLAGS, EXTRA_CPPFLAGS and EXTRA_CFLAGS are taken from the
+# environment.
+
+# Check command-line parameters and environment.
+: ${CC:=cc}
+output=${1:?missing output file name}
+
+# Generate pattern for C punctuators.
+punctuators=$(printf '%s' \
+'[ ] ( ) { } . ->
+++ -- & * + - ~ !
+/ % << >> < > <= >= == != ^ | && ||
+? : ; ...
+= *= /= %= += -= <<= >>= &= ^= |=
+, # ##
+<: :> <% %> %: %: %:' |
+	tr '\n' ' ' |
+	sed -ne 's/[[/.*\?]/\\&/g' -e 's/[[:space:]]\+/\\|/gp')
+
+# Generate include guard.
+guard=$(printf '%s_' "${output##*/}" |
+	tr [[:lower:]] [[:upper:]] |
+	sed -e 's/[^A-Z0-9]/_/g' -e 's/^.\{,4\}$//')
+
+# Retrieve C preprocessor output and expand it to one token per line.
+preprocess ()
+{
+	{
+		temp=/tmp/${0##*/}.$$.c
+		printf '#include "rte_flow.h"' > "$temp"
+		${CC} ${CPPFLAGS} ${EXTRA_CPPFLAGS} \
+			${CFLAGS} ${EXTRA_CFLAGS} -E "$temp"
+		rm -f "$temp"
+	} |
+	sed -e '
+/^[[:space:]]*#/d
+s/[[:space:]]\+/\n/g
+s/'"$punctuators"'/\n&\n/g
+' |
+	sed -e '/^[[:space:]]*$/d'
+}
+
+# Retrieve defined pattern items and actions.
+items=''
+actions=''
+while read -r type name dummy
+do
+	case "$type" in
+	ITEM)
+		items="$items $name"
+		;;
+	ACTION)
+		actions="$actions $name"
+		;;
+	esac
+done <<EOF
+$(preprocess |
+sed -ne '
+/^enum$/{
+	n
+	/rte_flow_\(item\|action\)_type/{
+		n
+		/{/{
+			:a
+			n
+			/}/b
+			s/^RTE_FLOW_\(ITEM\|ACTION\)_TYPE_\([A-Z_0-9]\+\)$/\1 \2/
+			p
+			ba
+		}
+	}
+}')
+EOF
+
+# Complain if something went wrong.
+: ${guard:?include guard is either too short or empty}
+: ${items:?no items found in input file}
+: ${actions:?no actions found in input file}
+
+# Open output file.
+exec 1> "${output}" ||
+exit
+
+# Reuse license header from this script.
+sed -ne '
+/^#.*BSD LICENSE/{
+	i\
+/*-
+	:a
+	/^#/!{
+		i\
+ */
+		q
+	}
+	s/^#/ */
+	p
+	n
+	ba
+}
+' "$0"
+
+# Output includes and structure definitions.
+printf '
+/**
+ * @file
+ * RTE generic flow API (internal)
+ *
+ * This file exports resources needed by rte_flow_conv().
+ *
+ * DO NOT EDIT THIS FILE.
+ *
+ * It was generated from rte_flow.h, run %s to update it.
+ */
+
+#ifndef %s
+#define %s
+
+#include <stddef.h>
+
+#include "rte_flow.h"
+
+/**
+ * This structure describes either a pattern item or an action as well as
+ * their associated specification or configuration structure.
+ */
+struct rte_flow_conv_res {
+	const char *name; /**< Object name. */
+	const void *mask; /**< Default mask for structure if relevant. */
+	size_t size; /**< Associated structure size. */
+	size_t flex_len_type; /**< Length field type for flexible array. */
+	size_t flex_len_off; /**< Length field offset for flexible array. */
+	size_t flex_elt_size; /**< Flexible array element size. */
+	size_t flex_off; /**< Flexible array field offset. */
+};
+' \
+	"${0##*/}" \
+	"$guard" \
+	"$guard"
+
+# Pattern items/actions arrays generator.
+generate ()
+{
+	type=${1:?missing type}
+	desc=${2:?missing description}
+	lc_type=$(printf '%s' "$type" | tr [:upper:] [:lower:])
+	: ${lc_type:?cannot convert type to lower case}
+	shift 2
+	objects=${@:?missing objects}
+	printf '
+/** %s description table. */
+static const struct rte_flow_conv_res rte_flow_conv_res_%s[] = {' \
+		"$desc" "$lc_type"
+	for object in $objects
+	do
+		lc_object=$(printf '%s' "$object" | tr [:upper:] [:lower:]) ||
+		break
+		set -- $(preprocess |
+		sed -ne '
+/^struct$/{
+	n
+	/^rte_flow_'"$lc_type"'_'"$lc_object"'$/{
+		:a
+		p
+		/^{$/{
+			H
+			n
+			ba
+		}
+		/^}$/{
+			x
+			s/\n{$//
+			/^$/q
+			x
+		}
+		n
+		ba
+	}
+}' |
+		tr '\n' ' ' |
+		sed -ne '
+s/ /\n/
+P
+s/^.* \([^ ]\+\) ; [^ ]\+ \([^ ]\+\) \[ ] ; } $/\1 \2/p')
+		size=${1:+"sizeof(struct $1)"}
+		: ${size:=0}
+		flex_len_type=${2:+"sizeof(((struct $1 *)0)->$2)"}
+		: ${flex_len_type:=0}
+		flex_len_off=${2:+"offsetof(struct $1, $2)"}
+		: ${flex_len_off:=0}
+		flex_elt_size=${2:+"sizeof(((struct $1 *)0)->$3[0])"}
+		: ${flex_elt_size:=0}
+		flex_off=${3:+"offsetof(struct $1, $3)"}
+		: ${flex_off:=0}
+		printf '
+	[RTE_FLOW_'"$type"'_TYPE_%s] = {
+		.name = "%s",
+		.size = %s,
+		.flex_len_type = %s,
+		.flex_len_off = %s,
+		.flex_elt_size = %s,
+		.flex_off = %s,
+	},' \
+		"$object" \
+		"$lc_object" \
+		"$size" \
+		"$flex_len_type" \
+		"$flex_len_off" \
+		"$flex_elt_size" \
+		"$flex_off"
+	done
+	printf '
+};
+'
+}
+
+# Generate pattern items/actions arrays.
+generate ITEM "Pattern items" $items &&
+generate ACTION "Actions" $actions ||
+exit
+
+# Output footer.
+printf '
+#endif /* %s */
+' \
+	"$guard"
diff --git a/lib/librte_ether/Makefile b/lib/librte_ether/Makefile
index 5f65d05..0b3f77e 100644
--- a/lib/librte_ether/Makefile
+++ b/lib/librte_ether/Makefile
@@ -61,4 +61,14 @@ SYMLINK-y-include += rte_flow_driver.h
 SYMLINK-y-include += rte_tm.h
 SYMLINK-y-include += rte_tm_driver.h
 
+#
+# This file is versioned thus not built automatically.
+# To re-generate it, run:
+#
+#     make lib/librte_ether_sub RTE_MAKE_SUBTARGET=rte_flow_conv.h
+#
+rte_flow_conv.h: rte_flow.h
+	@$(if $V,,echo "  GEN $(@F)")
+	$Q sh -- $(RTE_SDK)/buildtools/gen-rte_flow_conv-h.sh "$(<D)/$(@F)"
+
 include $(RTE_SDK)/mk/rte.lib.mk
-- 
2.1.4



More information about the dev mailing list