From d9ba1d35de4a10ae90076bb528567c9dff0cceb7 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 9 Mar 2009 11:25:37 -0700 Subject: [PATCH] Move ODP-related functions into new module "odp-util". This allows code outside ofproto.c to use it, which will soon be useful to allow vswitchd to hook OFPP_NORMAL. --- lib/automake.mk | 2 + lib/odp-util.c | 68 ++++++++++++++++++++++++++++++ lib/odp-util.h | 76 +++++++++++++++++++++++++++++++++ secchan/ofproto.c | 105 ++++++++++------------------------------------ secchan/ofproto.h | 1 + 5 files changed, 169 insertions(+), 83 deletions(-) create mode 100644 lib/odp-util.c create mode 100644 lib/odp-util.h diff --git a/lib/automake.mk b/lib/automake.mk index 1c2ed292..0b3738e1 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -45,6 +45,8 @@ lib_libopenflow_a_SOURCES = \ lib/mac-learning.h \ lib/netdev.c \ lib/netdev.h \ + lib/odp-util.c \ + lib/odp-util.h \ lib/ofp-print.c \ lib/ofp-print.h \ lib/ofpbuf.c \ diff --git a/lib/odp-util.c b/lib/odp-util.c new file mode 100644 index 00000000..5522aec4 --- /dev/null +++ b/lib/odp-util.c @@ -0,0 +1,68 @@ +/* Copyright (c) 2009 The Board of Trustees of The Leland Stanford Junior + * University + * + * We are making the OpenFlow specification and associated documentation + * (Software) available for public use and benefit with the expectation + * that others will use, modify and enhance the Software and contribute + * those enhancements back to the community. However, since we would + * like to make the Software available for broadest use, with as few + * restrictions as possible permission is hereby granted, free of + * charge, to any person obtaining a copy of this Software to deal in + * the Software under the copyrights 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. + * + * The name and trademarks of copyright holder(s) may NOT be used in + * advertising or publicity pertaining to the Software or any + * derivatives without specific, written prior permission. + */ + +#include +#include "odp-util.h" +#include +#include +#include "util.h" + +void +odp_actions_init(struct odp_actions *actions) +{ + actions->actions = NULL; + actions->n_actions = 0; + actions->allocated_actions = 0; +} + +void +odp_actions_free(struct odp_actions *actions) +{ + free(actions->actions); +} + +union odp_action * +odp_actions_add(struct odp_actions *actions, uint16_t type) +{ + union odp_action *a; + if (actions->n_actions >= actions->allocated_actions) { + actions->actions = x2nrealloc(actions->actions, + &actions->allocated_actions, + sizeof *actions->actions); + } + a = &actions->actions[actions->n_actions++]; + memset(a, 0, sizeof *a); + a->type = type; + return a; +} + diff --git a/lib/odp-util.h b/lib/odp-util.h new file mode 100644 index 00000000..3c33794e --- /dev/null +++ b/lib/odp-util.h @@ -0,0 +1,76 @@ +/* Copyright (c) 2009 The Board of Trustees of The Leland Stanford Junior + * University + * + * We are making the OpenFlow specification and associated documentation + * (Software) available for public use and benefit with the expectation + * that others will use, modify and enhance the Software and contribute + * those enhancements back to the community. However, since we would + * like to make the Software available for broadest use, with as few + * restrictions as possible permission is hereby granted, free of + * charge, to any person obtaining a copy of this Software to deal in + * the Software under the copyrights 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. + * + * The name and trademarks of copyright holder(s) may NOT be used in + * advertising or publicity pertaining to the Software or any + * derivatives without specific, written prior permission. + */ + +#ifndef ODP_UTIL_H +#define ODP_UTIL_H 1 + +#include +#include "openflow/datapath-protocol.h" +#include "openflow/openflow.h" + +struct odp_actions { + union odp_action *actions; + size_t n_actions, allocated_actions; +}; + +void odp_actions_init(struct odp_actions *); +void odp_actions_free(struct odp_actions *); +union odp_action *odp_actions_add(struct odp_actions *, uint16_t type); + +static inline uint16_t +ofp_port_to_odp_port(uint16_t ofp_port) +{ + switch (ofp_port) { + case OFPP_LOCAL: + return ODPP_LOCAL; + case OFPP_NONE: + return ODPP_NONE; + default: + return ofp_port; + } +} + +static inline uint16_t +odp_port_to_ofp_port(uint16_t odp_port) +{ + switch (odp_port) { + case ODPP_LOCAL: + return OFPP_LOCAL; + case ODPP_NONE: + return OFPP_NONE; + default: + return odp_port; + } +} + +#endif /* odp-util.h */ diff --git a/secchan/ofproto.c b/secchan/ofproto.c index 5b0c5044..6f466666 100644 --- a/secchan/ofproto.c +++ b/secchan/ofproto.c @@ -47,6 +47,7 @@ #include "in-band.h" #include "netdev.h" #include "netflow.h" +#include "odp-util.h" #include "ofp-print.h" #include "ofpbuf.h" #include "openflow/datapath-protocol.h" @@ -87,13 +88,6 @@ struct ofport { static void ofport_free(struct ofport *); static void hton_ofp_phy_port(struct ofp_phy_port *); -struct odp_actions { - union odp_action *actions; - size_t n_actions, allocated_actions; -}; - -static void init_actions(struct odp_actions *); -static void free_actions(struct odp_actions *); static void xlate_actions(const union ofp_action *in, size_t n_in, const flow_t *flow, struct ofproto *ofproto, struct odp_actions *out); @@ -761,7 +755,7 @@ ofproto_send_packet(struct ofproto *p, const flow_t *flow, xlate_actions(actions, n_actions, flow, p, &odp_actions); error = dpif_execute(&p->dpif, flow->in_port, odp_actions.actions, odp_actions.n_actions, packet); - free_actions(&odp_actions); + odp_actions_free(&odp_actions); return error; } @@ -807,7 +801,7 @@ ofproto_setup_exact_flow(struct ofproto *p, const flow_t *flow, odp_flow.actions = odp_actions.actions; odp_flow.n_actions = odp_actions.n_actions; dpif_flow_add(&p->dpif, &odp_flow); - free_actions(&odp_actions); + odp_actions_free(&odp_actions); } static void @@ -1324,78 +1318,23 @@ handle_set_config(struct ofproto *p, struct ofconn *ofconn, return 0; } -static uint16_t -ofp_port_to_odp_port(uint16_t ofp_port) -{ - switch (ofp_port) { - case OFPP_LOCAL: - return ODPP_LOCAL; - case OFPP_NONE: - return ODPP_NONE; - default: - return ofp_port; - } -} - -static uint16_t -odp_port_to_ofp_port(uint16_t odp_port) -{ - switch (odp_port) { - case ODPP_LOCAL: - return OFPP_LOCAL; - case ODPP_NONE: - return OFPP_NONE; - default: - return odp_port; - } -} - -static void -init_actions(struct odp_actions *actions) -{ - actions->actions = NULL; - actions->n_actions = 0; - actions->allocated_actions = 0; -} - -static void -free_actions(struct odp_actions *actions) -{ - free(actions->actions); -} - -static union odp_action * -add_action(struct odp_actions *actions, uint16_t type) -{ - union odp_action *a; - if (actions->n_actions >= actions->allocated_actions) { - actions->actions = x2nrealloc(actions->actions, - &actions->allocated_actions, - sizeof *actions->actions); - } - a = &actions->actions[actions->n_actions++]; - memset(a, 0, sizeof *a); - a->type = type; - return a; -} - static void add_output_action(struct odp_actions *actions, uint16_t port) { - add_action(actions, ODPAT_OUTPUT)->output.port = port; + odp_actions_add(actions, ODPAT_OUTPUT)->output.port = port; } static void add_output_group_action(struct odp_actions *actions, uint16_t group) { - add_action(actions, ODPAT_OUTPUT_GROUP)->output_group.group = group; + odp_actions_add(actions, ODPAT_OUTPUT_GROUP)->output_group.group = group; } static void add_controller_action(struct odp_actions *actions, const struct ofp_action_output *oao) { - union odp_action *a = add_action(actions, ODPAT_CONTROLLER); + union odp_action *a = odp_actions_add(actions, ODPAT_CONTROLLER); a->controller.arg = oao->max_len ? ntohs(oao->max_len) : UINT32_MAX; } @@ -1503,7 +1442,7 @@ xlate_nicira_action(const struct action_xlate_ctx *ctx, switch (subtype) { case NXAST_SNAT: nas = (const struct nx_action_snat *) nah; - oa = add_action(ctx->out, ODPAT_SNAT); + oa = odp_actions_add(ctx->out, ODPAT_SNAT); oa->snat.port = ntohs(nas->port); break; @@ -1536,38 +1475,38 @@ do_xlate_actions(struct action_xlate_ctx *ctx) break; case OFPAT_SET_VLAN_VID: - oa = add_action(ctx->out, ODPAT_SET_VLAN_VID); + oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_VID); oa->vlan_vid.vlan_vid = ia->vlan_vid.vlan_vid; break; case OFPAT_SET_VLAN_PCP: - oa = add_action(ctx->out, ODPAT_SET_VLAN_PCP); + oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_PCP); oa->vlan_pcp.vlan_pcp = ia->vlan_pcp.vlan_pcp; break; case OFPAT_STRIP_VLAN: - add_action(ctx->out, ODPAT_STRIP_VLAN); + odp_actions_add(ctx->out, ODPAT_STRIP_VLAN); break; case OFPAT_SET_DL_SRC: - oa = add_action(ctx->out, ODPAT_SET_DL_SRC); + oa = odp_actions_add(ctx->out, ODPAT_SET_DL_SRC); memcpy(oa->dl_addr.dl_addr, ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN); break; case OFPAT_SET_DL_DST: - oa = add_action(ctx->out, ODPAT_SET_DL_DST); + oa = odp_actions_add(ctx->out, ODPAT_SET_DL_DST); memcpy(oa->dl_addr.dl_addr, ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN); break; case OFPAT_SET_NW_SRC: - oa = add_action(ctx->out, ODPAT_SET_NW_SRC); + oa = odp_actions_add(ctx->out, ODPAT_SET_NW_SRC); oa->nw_addr.nw_addr = ia->nw_addr.nw_addr; break; case OFPAT_SET_TP_SRC: - oa = add_action(ctx->out, ODPAT_SET_TP_SRC); + oa = odp_actions_add(ctx->out, ODPAT_SET_TP_SRC); oa->tp_port.tp_port = ia->tp_port.tp_port; break; @@ -1588,7 +1527,7 @@ xlate_actions(const union ofp_action *in, size_t n_in, struct odp_actions *out) { struct action_xlate_ctx ctx; - init_actions(out); + odp_actions_init(out); ctx.in = in; ctx.n_in = n_in; ctx.flow = flow; @@ -1631,7 +1570,7 @@ handle_packet_out(struct ofproto *p, struct ofconn *ofconn, p, &actions); dpif_execute(&p->dpif, flow.in_port, actions.actions, actions.n_actions, &payload); - free_actions(&actions); + odp_actions_free(&actions); ofpbuf_delete(buffer); return 0; @@ -2107,7 +2046,7 @@ send_buffered(struct ofproto *p, struct ofconn *ofconn, uint32_t buffer_id, if (!error) { *byte_count = packet->size; } - free_actions(&actions); + odp_actions_free(&actions); ofpbuf_delete(packet); return 0; @@ -2180,7 +2119,7 @@ add_flow(struct ofproto *p, struct ofconn *ofconn, } rule_destroy(displaced_rule); } - free_actions(&actions); + odp_actions_free(&actions); } return buffer_error; } @@ -2218,7 +2157,7 @@ modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm, odp_flow.actions = actions.actions; odp_flow.n_actions = actions.n_actions; dpif_flow_add(&p->dpif, &odp_flow); - free_actions(&actions); + odp_actions_free(&actions); update_stats(rule, &odp_flow.stats); } @@ -2520,7 +2459,7 @@ handle_odp_msg(struct ofproto *p, struct ofpbuf *packet) rule_make_actions(p, old_sr, &actions); dpif_execute(&p->dpif, msg->port, actions.actions, actions.n_actions, &payload); - free_actions(&actions); + odp_actions_free(&actions); ofpbuf_delete(packet); return; } else { @@ -2549,7 +2488,7 @@ handle_odp_msg(struct ofproto *p, struct ofpbuf *packet) /* Execute subrule on packet. */ dpif_execute(&p->dpif, msg->port, actions.actions, actions.n_actions, &payload); - free_actions(&actions); + odp_actions_free(&actions); ofpbuf_delete(packet); } @@ -2594,7 +2533,7 @@ revalidate_subrule(struct ofproto *p, struct rule *sub) rule_make_actions(p, sub, &actions); dpif_flow_set_actions(&p->dpif, flow, actions.actions, actions.n_actions); - free_actions(&actions); + odp_actions_free(&actions); } } return true; diff --git a/secchan/ofproto.h b/secchan/ofproto.h index 54bca7ad..e3f57683 100644 --- a/secchan/ofproto.h +++ b/secchan/ofproto.h @@ -39,6 +39,7 @@ #include #include "flow.h" +struct odp_actions; struct ofproto; struct svec; -- 2.30.2