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