Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / autopath.c
1 /*
2  * Copyright (c) 2011 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-errors.h"
28 #include "ofp-util.h"
29 #include "openflow/nicira-ext.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(autopath);
33
34 /* Loads 'ofp_port' into the appropriate register in accordance with the
35  * autopath action. */
36 void
37 autopath_execute(const struct nx_action_autopath *ap, struct flow *flow,
38                  uint16_t ofp_port)
39 {
40     struct mf_subfield dst;
41
42     nxm_decode(&dst, ap->dst, ap->ofs_nbits);
43     mf_set_subfield_value(&dst, ofp_port, flow);
44 }
45
46 void
47 autopath_parse(struct nx_action_autopath *ap, const char *s_)
48 {
49     char *s;
50     char *id_str, *dst_s, *save_ptr;
51     struct mf_subfield dst;
52     int id_int;
53
54     s = xstrdup(s_);
55     save_ptr = NULL;
56     id_str = strtok_r(s, ", ", &save_ptr);
57     dst_s = strtok_r(NULL, ", ", &save_ptr);
58
59     if (!dst_s) {
60         ovs_fatal(0, "%s: not enough arguments to autopath action", s_);
61     }
62
63     id_int = atoi(id_str);
64     if (id_int < 1 || id_int > UINT32_MAX) {
65         ovs_fatal(0, "%s: autopath id %d is not in valid range "
66                   "1 to %"PRIu32, s_, id_int, UINT32_MAX);
67     }
68
69     mf_parse_subfield(&dst, dst_s);
70     if (dst.n_bits < 16) {
71         ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
72                   "less than required 65536",
73                   s_, dst.n_bits, 1u << dst.n_bits);
74     }
75
76     ofputil_init_NXAST_AUTOPATH(ap);
77     ap->id = htonl(id_int);
78     ap->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
79     ap->dst = htonl(dst.field->nxm_header);
80
81     free(s);
82 }
83
84 enum ofperr
85 autopath_check(const struct nx_action_autopath *ap, const struct flow *flow)
86 {
87     struct mf_subfield dst;
88
89     nxm_decode(&dst, ap->dst, ap->ofs_nbits);
90     if (dst.n_bits < 16) {
91         VLOG_WARN("at least 16 bit destination is required for autopath "
92                   "action.");
93         return OFPERR_OFPBAC_BAD_ARGUMENT;
94     }
95
96     return mf_check_dst(&dst, flow);
97 }