From patchwork Tue Sep 26 05:36:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuanhan Liu X-Patchwork-Id: 818456 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=fridaylinux-org.20150623.gappssmtp.com header.i=@fridaylinux-org.20150623.gappssmtp.com header.b="KFKtnLKf"; dkim-atps=neutral Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3y1V9L57tkz9t30 for ; Tue, 26 Sep 2017 15:38:14 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 87F82B5D; Tue, 26 Sep 2017 05:38:11 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id D2A11B13 for ; Tue, 26 Sep 2017 05:38:10 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pg0-f47.google.com (mail-pg0-f47.google.com [74.125.83.47]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 297CDD3 for ; Tue, 26 Sep 2017 05:38:10 +0000 (UTC) Received: by mail-pg0-f47.google.com with SMTP id p5so5358784pgn.7 for ; Mon, 25 Sep 2017 22:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fridaylinux-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hbLoeIdpU7FHgEp+drPGWc9ybiGBWF+mAcK/J5A5/Dk=; b=KFKtnLKfFheO1st2vETXFzux70IFxT2ooEZtYeoyztojVo9uSPX7FhCEyA3CArx3D6 ibmjUDWlyf/WXMADo2+U326e1KisBv19Cyb3a6UqmGjoyxyUZ8uhayEeVgG7coWXAdMF KIhS6/x75pbHg7rEBL8EV0MU0MwxipOAQZUzApWful9Fnr/Y9o7kXcthtcOcTMTFcmAM eVPYXtDafAeqdqEAMDvZzhKzlTcHUa2PLAqhcnNz1WnbNC7aCJypdykyAJmCljsxHZJF W48yc0PvZl/15X2ouKPjLtNkWWCVznk29UorPDEsbrTJpJEpdqJsiGso8HVzh3PNG36g Fddw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hbLoeIdpU7FHgEp+drPGWc9ybiGBWF+mAcK/J5A5/Dk=; b=ua6Nk3clV+eAtqaRPjCBwa1/vQUGOo4OCmc28fJ/h76oUlv1kApseoaLTT+w5FnBut /4QbaJHTpYGrsv5ZX0863A2oS9Ao8Ff4ykPPDAl71uLZea6AVd+5bzW5yCW004TbBU1Y SFz5wuVQM19qENhwnuUfwG//0FbkLhipi5xxYujZ+6r0XBibDLcR2BNXUQW+lFoNXBtG 3aMUkM+gxG7g6oZGsv1e545Bs+nL9M8FGQMWZBukR1jh3enCvvq2CuKzpKaWPDee2tNJ foKkx1n3dVB9mPJivlRUlMGEd8/TabMUlr7CsOSc4YjaPcd2hIs8ytCeZRPAygqfefY9 kTLQ== X-Gm-Message-State: AHPjjUhlCdPrwCWhwSzvq5vB+Cn2W/Tf3RsfuwY8cyuT/04s9uDb5vZw Lf+ssX7uiboi/v1Ivv2Cdy6Q/dUfuObBdQ== X-Google-Smtp-Source: AOwi7QDdB4O6C91dZ0jMetu7n6OhasM8LjQBiKQYPU5emqp5F/UDtoD+DsvDIQHYTo3KFigwOuO2XQ== X-Received: by 10.99.185.26 with SMTP id z26mr9788097pge.438.1506404289302; Mon, 25 Sep 2017 22:38:09 -0700 (PDT) Received: from localhost.localdomain ([101.228.205.132]) by smtp.gmail.com with ESMTPSA id o79sm13180077pfi.108.2017.09.25.22.37.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 25 Sep 2017 22:38:08 -0700 (PDT) From: Yuanhan Liu To: dev@openvswitch.org Date: Tue, 26 Sep 2017 13:36:31 +0800 Message-Id: <1506404199-23579-2-git-send-email-yliu@fridaylinux.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1506404199-23579-1-git-send-email-yliu@fridaylinux.org> References: <1506404199-23579-1-git-send-email-yliu@fridaylinux.org> X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Simon Horman Subject: [ovs-dev] [PATCH v3 1/9] dpif-netdev: associate flow with a mark id X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org This patch associate a flow with a mark id (a uint32_t number) by a direct array. The array is initiated with 1024 slots at the begninning, and it will be enlarged when it's not big enough. The mark id is allocated with the help of id-pool. It re-uses the flow API (more precisely, the ->flow_put method) to setup the hw flow. The flow_put implementation then is supposed to create a flow with MARK action for current stage, which is, only partial offload is supported. Co-authored-by: Finn Christensen Signed-off-by: Yuanhan Liu Signed-off-by: Finn Christensen --- lib/dpif-netdev.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/netdev.h | 6 +++ 2 files changed, 146 insertions(+) diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 071ec14..f417b25 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -453,6 +453,9 @@ struct dp_netdev_flow { struct ovs_refcount ref_cnt; bool dead; + bool has_mark; /* A flag to tell whether this flow has a + valid mark asscoiated with it. */ + uint32_t mark; /* Unique flow mark assiged to a flow */ /* Statistics. */ struct dp_netdev_flow_stats stats; @@ -1833,6 +1836,82 @@ dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread *pmd, return cls; } +/* + * An array to map from the flow mark (the index) to dp netdev flow. + * Initial size is set to 1024, it will be resized when it's not + * big enough. + */ +struct flow_mark_map { + size_t max; + struct ovs_mutex mutex; + struct id_pool *mark_pool; + struct dp_netdev_flow **flows; +}; + +static struct flow_mark_map flow_mark_map = { + .max = 1024, + .mutex = OVS_MUTEX_INITIALIZER, +}; + +static bool +dp_netdev_alloc_flow_mark(uint32_t *mark) +{ + bool succeed = true; + + ovs_mutex_lock(&flow_mark_map.mutex); + + /* Haven't initiated yet, do it here */ + if (!flow_mark_map.flows) { + flow_mark_map.flows = xzalloc(sizeof(struct dp_netdev_flow *) * + flow_mark_map.max); + flow_mark_map.mark_pool = id_pool_create(0, UINT32_MAX / 2); + } + + do { + if (!id_pool_alloc_id(flow_mark_map.mark_pool, mark)) { + succeed = false; + break; + } + + if (*mark >= flow_mark_map.max) { + flow_mark_map.flows = xrealloc(flow_mark_map.flows, + flow_mark_map.max * 2 * + sizeof(struct dp_netdev_flow *)); + memset(&flow_mark_map.flows[flow_mark_map.max], 0, + flow_mark_map.max * sizeof(struct dp_netdev_flow *)); + flow_mark_map.max *= 2; + + break; + } + } while (flow_mark_map.flows[*mark]); + + ovs_mutex_unlock(&flow_mark_map.mutex); + + return succeed; +} + +static void +dp_netdev_install_flow_mark_map(const uint32_t mark, + struct dp_netdev_flow *netdev_flow) +{ + ovs_mutex_lock(&flow_mark_map.mutex); + if (mark < flow_mark_map.max) { + flow_mark_map.flows[mark] = netdev_flow; + } + ovs_mutex_unlock(&flow_mark_map.mutex); +} + +static void +dp_netdev_remove_flow_mark_map(const uint32_t mark) +{ + ovs_mutex_lock(&flow_mark_map.mutex); + id_pool_free_id(flow_mark_map.mark_pool, mark); + if (mark < flow_mark_map.max) { + flow_mark_map.flows[mark] = NULL; + } + ovs_mutex_unlock(&flow_mark_map.mutex); +} + static void dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd, struct dp_netdev_flow *flow) @@ -1846,6 +1925,10 @@ dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd, ovs_assert(cls != NULL); dpcls_remove(cls, &flow->cr); cmap_remove(&pmd->flow_table, node, dp_netdev_flow_hash(&flow->ufid)); + if (flow->has_mark) { + dp_netdev_remove_flow_mark_map(flow->mark); + flow->has_mark = false; + } flow->dead = true; dp_netdev_flow_unref(flow); @@ -2425,6 +2508,55 @@ out: return error; } +static void +try_netdev_flow_put(struct dp_netdev_pmd_thread *pmd, odp_port_t in_port, + struct dp_netdev_flow *flow, struct match *match, + const ovs_u128 *ufid, const struct nlattr *actions, + size_t actions_len) +{ + struct offload_info info; + struct dp_netdev_port *port; + bool modification = flow->has_mark; + const char *op = modification ? "modify" : "install"; + int ret; + + port = dp_netdev_lookup_port(pmd->dp, in_port); + if (!port) { + return; + } + + if (modification) { + info.flow_mark = flow->mark; + } else { + if (!netdev_is_flow_api_enabled()) { + return; + } + + if (!dp_netdev_alloc_flow_mark(&info.flow_mark)) { + VLOG_ERR("failed to allocate flow mark!\n"); + return; + } + } + + ret = netdev_flow_put(port->netdev, match, + CONST_CAST(struct nlattr *, actions), + actions_len, ufid, &info, NULL); + if (ret) { + VLOG_ERR("failed to %s netdev flow with mark %u\n", + op, info.flow_mark); + return; + } + + if (!modification) { + flow->has_mark = true; + flow->mark = info.flow_mark; + dp_netdev_install_flow_mark_map(info.flow_mark, flow); + } + + VLOG_INFO("succeed to %s netdev flow with mark %u\n", + op, flow->mark); +} + static struct dp_netdev_flow * dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd, struct match *match, const ovs_u128 *ufid, @@ -2460,6 +2592,7 @@ dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd, memset(&flow->stats, 0, sizeof flow->stats); flow->dead = false; flow->batch = NULL; + flow->has_mark = false; *CONST_CAST(unsigned *, &flow->pmd_id) = pmd->core_id; *CONST_CAST(struct flow *, &flow->flow) = match->flow; *CONST_CAST(ovs_u128 *, &flow->ufid) = *ufid; @@ -2475,6 +2608,9 @@ dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd, cmap_insert(&pmd->flow_table, CONST_CAST(struct cmap_node *, &flow->node), dp_netdev_flow_hash(&flow->ufid)); + try_netdev_flow_put(pmd, in_port, flow, match, ufid, + actions, actions_len); + if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl)))) { struct ds ds = DS_EMPTY_INITIALIZER; struct ofpbuf key_buf, mask_buf; @@ -2555,6 +2691,7 @@ flow_put_on_pmd(struct dp_netdev_pmd_thread *pmd, if (put->flags & DPIF_FP_MODIFY) { struct dp_netdev_actions *new_actions; struct dp_netdev_actions *old_actions; + odp_port_t in_port = netdev_flow->flow.in_port.odp_port; new_actions = dp_netdev_actions_create(put->actions, put->actions_len); @@ -2562,6 +2699,9 @@ flow_put_on_pmd(struct dp_netdev_pmd_thread *pmd, old_actions = dp_netdev_flow_get_actions(netdev_flow); ovsrcu_set(&netdev_flow->actions, new_actions); + try_netdev_flow_put(pmd, in_port, netdev_flow, match, ufid, + put->actions, put->actions_len); + if (stats) { get_dpif_flow_stats(netdev_flow, stats); } diff --git a/lib/netdev.h b/lib/netdev.h index f8482f7..2003165 100644 --- a/lib/netdev.h +++ b/lib/netdev.h @@ -188,6 +188,12 @@ void netdev_send_wait(struct netdev *, int qid); struct offload_info { const struct dpif_class *dpif_class; ovs_be16 tp_dst_port; /* Destination port for tunnel in SET action */ + + /* + * The flow mark id assigened to the flow. If any pkts hit the flow, + * it will be in the pkt meta data. + */ + uint32_t flow_mark; }; struct dpif_class; struct netdev_flow_dump;