ovs-ofctl: Accept port keywords, OF1.1 port numbers, reject port number 0.
[sliver-openvswitch.git] / lib / autopath.c
1 /*
2  * Copyright (c) 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "autopath.h"
20
21 #include <inttypes.h>
22 #include <stdlib.h>
23
24 #include "flow.h"
25 #include "meta-flow.h"
26 #include "nx-match.h"
27 #include "ofp-actions.h"
28 #include "ofp-errors.h"
29 #include "ofp-util.h"
30 #include "openflow/nicira-ext.h"
31 #include "vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(autopath);
34
35 void
36 autopath_parse(struct ofpact_autopath *ap, const char *s_)
37 {
38     char *s;
39     char *id_str, *dst, *save_ptr;
40
41     ofpact_init_AUTOPATH(ap);
42
43     s = xstrdup(s_);
44     save_ptr = NULL;
45     id_str = strtok_r(s, ", ", &save_ptr);
46     dst = strtok_r(NULL, ", ", &save_ptr);
47
48     if (!dst) {
49         ovs_fatal(0, "%s: not enough arguments to autopath action", s_);
50     }
51
52     ap->port = ofputil_port_from_string(id_str);
53     if (!ap->port) {
54         ovs_fatal(0, "%s: bad port number", s_);
55     }
56
57     mf_parse_subfield(&ap->dst, dst);
58     if (ap->dst.n_bits < 16) {
59         ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
60                   "less than required 65536",
61                   s_, ap->dst.n_bits, 1u << ap->dst.n_bits);
62     }
63
64     free(s);
65 }
66
67 enum ofperr
68 autopath_from_openflow(const struct nx_action_autopath *nap,
69                        struct ofpact_autopath *autopath)
70 {
71     ofpact_init_AUTOPATH(autopath);
72     autopath->dst.field = mf_from_nxm_header(ntohl(nap->dst));
73     autopath->dst.ofs = nxm_decode_ofs(nap->ofs_nbits);
74     autopath->dst.n_bits = nxm_decode_n_bits(nap->ofs_nbits);
75     autopath->port = ntohl(nap->id);
76
77     if (autopath->dst.n_bits < 16) {
78         VLOG_WARN("at least 16 bit destination is required for autopath "
79                   "action.");
80         return OFPERR_OFPBAC_BAD_ARGUMENT;
81     }
82
83     return autopath_check(autopath, NULL);
84 }
85
86 enum ofperr
87 autopath_check(const struct ofpact_autopath *autopath, const struct flow *flow)
88 {
89     VLOG_WARN_ONCE("The autopath action is deprecated and may be removed in"
90                    " February 2013.  Please email dev@openvswitch.org with"
91                    " concerns.");
92     return mf_check_dst(&autopath->dst, flow);
93 }
94
95 void
96 autopath_to_nxast(const struct ofpact_autopath *autopath,
97                   struct ofpbuf *openflow)
98 {
99     struct nx_action_autopath *ap;
100
101     ap = ofputil_put_NXAST_AUTOPATH__DEPRECATED(openflow);
102     ap->ofs_nbits = nxm_encode_ofs_nbits(autopath->dst.ofs,
103                                          autopath->dst.n_bits);
104     ap->dst = htonl(autopath->dst.field->nxm_header);
105     ap->id = htonl(autopath->port);
106 }