Skip to content

Commit 83c8df2

Browse files
author
Pravin B Shelar
committed
openvswitch: refactor ovs flow extract API.
OVS flow extract is called on packet receive or packet execute code path. Following patch defines separate API for extracting flow-key in packet execute code path. Signed-off-by: Pravin B Shelar <pshelar@nicira.com> Acked-by: Andy Zhou <azhou@nicira.com>
1 parent 2ff3e4e commit 83c8df2

7 files changed

Lines changed: 71 additions & 41 deletions

File tree

‎net/openvswitch/datapath.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ void ovs_dp_detach_port(struct vport *p)
237237
}
238238

239239
/* Must be called with rcu_read_lock. */
240-
void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
240+
void ovs_dp_process_received_packet(struct sk_buff *skb)
241241
{
242+
const struct vport *p = OVS_CB(skb)->input_vport;
242243
struct datapath *dp = p->dp;
243244
struct sw_flow *flow;
244245
struct dp_stats_percpu *stats;
@@ -250,7 +251,7 @@ void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
250251
stats = this_cpu_ptr(dp->stats_percpu);
251252

252253
/* Extract flow from 'skb' into 'key'. */
253-
error = ovs_flow_extract(skb, p->port_no, &key);
254+
error = ovs_flow_key_extract(skb, &key);
254255
if (unlikely(error)) {
255256
kfree_skb(skb);
256257
return;
@@ -514,6 +515,7 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
514515
struct sw_flow *flow;
515516
struct datapath *dp;
516517
struct ethhdr *eth;
518+
struct vport *input_vport;
517519
int len;
518520
int err;
519521

@@ -548,13 +550,11 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
548550
if (IS_ERR(flow))
549551
goto err_kfree_skb;
550552

551-
err = ovs_flow_extract(packet, -1, &flow->key);
553+
err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
554+
&flow->key);
552555
if (err)
553556
goto err_flow_free;
554557

555-
err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
556-
if (err)
557-
goto err_flow_free;
558558
acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
559559
err = PTR_ERR(acts);
560560
if (IS_ERR(acts))
@@ -576,6 +576,15 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
576576
if (!dp)
577577
goto err_unlock;
578578

579+
input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
580+
if (!input_vport)
581+
input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
582+
583+
if (!input_vport)
584+
goto err_unlock;
585+
586+
OVS_CB(packet)->input_vport = input_vport;
587+
579588
local_bh_disable();
580589
err = ovs_execute_actions(dp, packet, &flow->key);
581590
local_bh_enable();

‎net/openvswitch/datapath.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ struct datapath {
9797
* @flow: The flow associated with this packet. May be %NULL if no flow.
9898
* @tun_key: Key for the tunnel that encapsulated this packet. NULL if the
9999
* packet is not being tunneled.
100+
* @input_vport: The original vport packet came in on. This value is cached
101+
* when a packet is received by OVS.
100102
*/
101103
struct ovs_skb_cb {
102104
struct sw_flow *flow;
103105
struct ovs_key_ipv4_tunnel *tun_key;
106+
struct vport *input_vport;
104107
};
105108
#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
106109

@@ -181,7 +184,7 @@ static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_n
181184
extern struct notifier_block ovs_dp_device_notifier;
182185
extern struct genl_family dp_vport_genl_family;
183186

184-
void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
187+
void ovs_dp_process_received_packet(struct sk_buff *);
185188
void ovs_dp_detach_port(struct vport *);
186189
int ovs_dp_upcall(struct datapath *, struct sk_buff *,
187190
const struct dp_upcall_info *);

‎net/openvswitch/flow.c‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* 02110-1301, USA
1717
*/
1818

19-
#include "flow.h"
20-
#include "datapath.h"
2119
#include <linux/uaccess.h>
2220
#include <linux/netdevice.h>
2321
#include <linux/etherdevice.h>
@@ -46,6 +44,10 @@
4644
#include <net/ipv6.h>
4745
#include <net/ndisc.h>
4846

47+
#include "datapath.h"
48+
#include "flow.h"
49+
#include "flow_netlink.h"
50+
4951
u64 ovs_flow_used_time(unsigned long flow_jiffies)
5052
{
5153
struct timespec cur_ts;
@@ -420,10 +422,9 @@ static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
420422
}
421423

422424
/**
423-
* ovs_flow_extract - extracts a flow key from an Ethernet frame.
425+
* key_extract - extracts a flow key from an Ethernet frame.
424426
* @skb: sk_buff that contains the frame, with skb->data pointing to the
425427
* Ethernet header
426-
* @in_port: port number on which @skb was received.
427428
* @key: output flow key
428429
*
429430
* The caller must ensure that skb->len >= ETH_HLEN.
@@ -442,19 +443,11 @@ static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
442443
* of a correct length, otherwise the same as skb->network_header.
443444
* For other key->eth.type values it is left untouched.
444445
*/
445-
int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
446+
static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
446447
{
447448
int error;
448449
struct ethhdr *eth;
449450

450-
memset(key, 0, sizeof(*key));
451-
452-
key->phy.priority = skb->priority;
453-
if (OVS_CB(skb)->tun_key)
454-
memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
455-
key->phy.in_port = in_port;
456-
key->phy.skb_mark = skb->mark;
457-
458451
skb_reset_mac_header(skb);
459452

460453
/* Link layer. We are guaranteed to have at least the 14 byte Ethernet
@@ -610,6 +603,34 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
610603
}
611604
}
612605
}
613-
614606
return 0;
615607
}
608+
609+
int ovs_flow_key_extract(struct sk_buff *skb, struct sw_flow_key *key)
610+
{
611+
/* Extract metadata from packet. */
612+
memset(key, 0, sizeof(*key));
613+
if (OVS_CB(skb)->tun_key)
614+
memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
615+
616+
key->phy.priority = skb->priority;
617+
key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
618+
key->phy.skb_mark = skb->mark;
619+
620+
return key_extract(skb, key);
621+
}
622+
623+
int ovs_flow_key_extract_userspace(const struct nlattr *attr,
624+
struct sk_buff *skb,
625+
struct sw_flow_key *key)
626+
{
627+
int err;
628+
629+
memset(key, 0, sizeof(*key));
630+
/* Extract metadata from netlink attributes. */
631+
err = ovs_nla_get_flow_metadata(attr, key);
632+
if (err)
633+
return err;
634+
635+
return key_extract(skb, key);
636+
}

‎net/openvswitch/flow.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ void ovs_flow_stats_get(const struct sw_flow *, struct ovs_flow_stats *,
187187
void ovs_flow_stats_clear(struct sw_flow *);
188188
u64 ovs_flow_used_time(unsigned long flow_jiffies);
189189

190-
int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);
190+
int ovs_flow_key_extract(struct sk_buff *skb, struct sw_flow_key *key);
191+
/* Extract key from packet coming from userspace. */
192+
int ovs_flow_key_extract_userspace(const struct nlattr *attr,
193+
struct sk_buff *skb,
194+
struct sw_flow_key *key);
191195

192196
#endif /* flow.h */

‎net/openvswitch/flow_netlink.c‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ int ovs_nla_get_match(struct sw_flow_match *match,
836836

837837
/**
838838
* ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
839-
* @flow: Receives extracted in_port, priority, tun_key and skb_mark.
839+
* @key: Receives extracted in_port, priority, tun_key and skb_mark.
840840
* @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
841841
* sequence.
842842
*
@@ -846,32 +846,24 @@ int ovs_nla_get_match(struct sw_flow_match *match,
846846
* extracted from the packet itself.
847847
*/
848848

849-
int ovs_nla_get_flow_metadata(struct sw_flow *flow,
850-
const struct nlattr *attr)
849+
int ovs_nla_get_flow_metadata(const struct nlattr *attr,
850+
struct sw_flow_key *key)
851851
{
852-
struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
853852
const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
853+
struct sw_flow_match match;
854854
u64 attrs = 0;
855855
int err;
856-
struct sw_flow_match match;
857-
858-
flow->key.phy.in_port = DP_MAX_PORTS;
859-
flow->key.phy.priority = 0;
860-
flow->key.phy.skb_mark = 0;
861-
memset(tun_key, 0, sizeof(flow->key.tun_key));
862856

863857
err = parse_flow_nlattrs(attr, a, &attrs);
864858
if (err)
865859
return -EINVAL;
866860

867861
memset(&match, 0, sizeof(match));
868-
match.key = &flow->key;
862+
match.key = key;
869863

870-
err = metadata_from_nlattrs(&match, &attrs, a, false);
871-
if (err)
872-
return err;
864+
key->phy.in_port = DP_MAX_PORTS;
873865

874-
return 0;
866+
return metadata_from_nlattrs(&match, &attrs, a, false);
875867
}
876868

877869
int ovs_nla_put_flow(const struct sw_flow_key *swkey,

‎net/openvswitch/flow_netlink.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ void ovs_match_init(struct sw_flow_match *match,
4242

4343
int ovs_nla_put_flow(const struct sw_flow_key *,
4444
const struct sw_flow_key *, struct sk_buff *);
45-
int ovs_nla_get_flow_metadata(struct sw_flow *flow,
46-
const struct nlattr *attr);
45+
int ovs_nla_get_flow_metadata(const struct nlattr *, struct sw_flow_key *);
46+
4747
int ovs_nla_get_match(struct sw_flow_match *match,
4848
const struct nlattr *,
4949
const struct nlattr *);

‎net/openvswitch/vport.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007-2012 Nicira, Inc.
2+
* Copyright (c) 2007-2014 Nicira, Inc.
33
*
44
* This program is free software; you can redistribute it and/or
55
* modify it under the terms of version 2 of the GNU General Public
@@ -443,7 +443,8 @@ void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
443443
u64_stats_update_end(&stats->syncp);
444444

445445
OVS_CB(skb)->tun_key = tun_key;
446-
ovs_dp_process_received_packet(vport, skb);
446+
OVS_CB(skb)->input_vport = vport;
447+
ovs_dp_process_received_packet(skb);
447448
}
448449

449450
/**

0 commit comments

Comments
 (0)