+/* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
+ * The caller must free '*bufp' when the reply is no longer needed ('reply'
+ * will contain pointers into '*bufp'). */
+static int
+dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
+ struct ofpbuf **bufp)
+{
+ struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+ struct dpif_linux_dp request;
+
+ dpif_linux_dp_init(&request);
+ request.cmd = OVS_DP_CMD_GET;
+ request.dp_ifindex = dpif->dp_ifindex;
+
+ return dpif_linux_dp_transact(&request, reply, bufp);
+}
+\f
+/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
+ * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
+ * positive errno value.
+ *
+ * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
+ * while 'flow' is still in use. */
+static int
+dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
+ const struct ofpbuf *buf)
+{
+ static const struct nl_policy ovs_flow_policy[] = {
+ [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
+ [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
+ [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
+ [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
+ .optional = true },
+ [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
+ [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
+ /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
+ };
+
+ struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
+ struct ovs_header *ovs_header;
+ struct nlmsghdr *nlmsg;
+ struct genlmsghdr *genl;
+ struct ofpbuf b;
+
+ dpif_linux_flow_init(flow);
+
+ ofpbuf_use_const(&b, buf->data, buf->size);
+ nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
+ genl = ofpbuf_try_pull(&b, sizeof *genl);
+ ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
+ if (!nlmsg || !genl || !ovs_header
+ || nlmsg->nlmsg_type != ovs_flow_family
+ || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
+ ARRAY_SIZE(ovs_flow_policy))) {
+ return EINVAL;
+ }
+
+ flow->nlmsg_flags = nlmsg->nlmsg_flags;
+ flow->dp_ifindex = ovs_header->dp_ifindex;
+ flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
+ flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
+
+ if (a[OVS_FLOW_ATTR_MASK]) {
+ flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
+ flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
+ }
+ if (a[OVS_FLOW_ATTR_ACTIONS]) {
+ flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
+ flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
+ }
+ if (a[OVS_FLOW_ATTR_STATS]) {
+ flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
+ }
+ if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
+ flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
+ }
+ if (a[OVS_FLOW_ATTR_USED]) {
+ flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
+ }
+ return 0;
+}
+
+/* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
+ * followed by Netlink attributes corresponding to 'flow'. */
+static void
+dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
+ struct ofpbuf *buf)
+{
+ struct ovs_header *ovs_header;
+
+ nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
+ NLM_F_REQUEST | flow->nlmsg_flags,
+ flow->cmd, OVS_FLOW_VERSION);
+
+ ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
+ ovs_header->dp_ifindex = flow->dp_ifindex;
+
+ if (flow->key_len) {
+ nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
+ }
+
+ if (flow->mask_len) {
+ nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
+ }
+
+ if (flow->actions || flow->actions_len) {
+ nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
+ flow->actions, flow->actions_len);
+ }
+
+ /* We never need to send these to the kernel. */
+ ovs_assert(!flow->stats);
+ ovs_assert(!flow->tcp_flags);
+ ovs_assert(!flow->used);
+
+ if (flow->clear) {
+ nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
+ }
+}
+
+/* Clears 'flow' to "empty" values. */