autopath: Create the autopath action.
[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-util.h"
27 #include "openflow/nicira-ext.h"
28 #include "vlog.h"
29
30 VLOG_DEFINE_THIS_MODULE(autopath);
31
32 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
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     uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(ap->dst))];
41     int ofs = nxm_decode_ofs(ap->ofs_nbits);
42     int n_bits = nxm_decode_n_bits(ap->ofs_nbits);
43     uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
44     *reg = (*reg & ~(mask << ofs)) | ((ofp_port & mask) << ofs);
45 }
46
47 void
48 autopath_parse(struct nx_action_autopath *ap, const char *s_)
49 {
50     char *s;
51     uint32_t reg;
52     int id_int, ofs, n_bits;
53     char *id_str, *dst, *save_ptr;
54
55     s = xstrdup(s_);
56     save_ptr = NULL;
57     id_str = strtok_r(s, ", ", &save_ptr);
58     dst = strtok_r(NULL, ", ", &save_ptr);
59
60     if (!dst) {
61         ovs_fatal(0, "%s: not enough arguments to autopath action", s_);
62     }
63
64     id_int = atoi(id_str);
65     if (id_int < 1 || id_int > UINT32_MAX) {
66         ovs_fatal(0, "%s: autopath id %d is not in valid range "
67                   "1 to %"PRIu32, s_, id_int, UINT32_MAX);
68     }
69
70     nxm_parse_field_bits(dst, &reg, &ofs, &n_bits);
71     if (!NXM_IS_NX_REG(reg) || NXM_NX_REG_IDX(reg) >= FLOW_N_REGS) {
72         ovs_fatal(0, "%s: destination field must be a register", s_);
73     }
74
75     if (n_bits < 16) {
76         ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
77                   "less than required 65536", s_, n_bits, 1u << n_bits);
78     }
79
80     memset(ap, 0, sizeof *ap);
81     ap->type = htons(OFPAT_VENDOR);
82     ap->len = htons(sizeof *ap);
83     ap->vendor = htonl(NX_VENDOR_ID);
84     ap->subtype = htons(NXAST_AUTOPATH);
85     ap->id = htonl(id_int);
86     ap->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
87     ap->dst = htonl(reg);
88
89     free(s);
90 }
91
92 int
93 autopath_check(const struct nx_action_autopath *ap)
94 {
95     uint32_t dst = ntohl(ap->dst);
96     int ofs = nxm_decode_ofs(ap->ofs_nbits);
97     int n_bits = nxm_decode_n_bits(ap->ofs_nbits);
98
99     if (!NXM_IS_NX_REG(dst) || NXM_NX_REG_IDX(dst) >= FLOW_N_REGS) {
100         VLOG_WARN_RL(&rl, "unsupported destination field %#"PRIx32, dst);
101     } else if (ofs + n_bits > nxm_field_bits(dst)) {
102         VLOG_WARN_RL(&rl, "destination overflows output field");
103     } else if (n_bits < 16) {
104         VLOG_WARN_RL(&rl, "minimum of 16 bits required in output field");
105     } else {
106         return 0;
107     }
108
109     return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
110 }