+
+static int
+parse_odp_action(const char *s, const struct simap *port_names,
+ struct ofpbuf *actions)
+{
+ /* Many of the sscanf calls in this function use oversized destination
+ * fields because some sscanf() implementations truncate the range of %i
+ * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
+ * value of 0x7fff. The other alternatives are to allow only a single
+ * radix (e.g. decimal or hexadecimal) or to write more sophisticated
+ * parsers.
+ *
+ * The tun_id parser has to use an alternative approach because there is no
+ * type larger than 64 bits. */
+
+ {
+ unsigned long long int port;
+ int n = -1;
+
+ if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
+ nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
+ return n;
+ }
+ }
+
+ if (port_names) {
+ int len = strcspn(s, delimiters);
+ struct simap_node *node;
+
+ node = simap_find_len(port_names, s, len);
+ if (node) {
+ nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
+ return len;
+ }
+ }
+
+ {
+ unsigned long long int pid;
+ unsigned long long int output;
+ char userdata_s[32];
+ int vid, pcp;
+ int n = -1;
+
+ if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
+ odp_put_userspace_action(pid, NULL, 0, actions);
+ return n;
+ } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
+ "pcp=%i,output=%lli))%n",
+ &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
+ union user_action_cookie cookie;
+ uint16_t tci;
+
+ tci = vid | (pcp << VLAN_PCP_SHIFT);
+ if (tci) {
+ tci |= VLAN_CFI;
+ }
+
+ cookie.type = USER_ACTION_COOKIE_SFLOW;
+ cookie.sflow.vlan_tci = htons(tci);
+ cookie.sflow.output = output;
+ odp_put_userspace_action(pid, &cookie, sizeof cookie, actions);
+ return n;
+ } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
+ && n > 0) {
+ union user_action_cookie cookie;
+ int res;
+
+ cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
+ cookie.slow_path.unused = 0;
+ cookie.slow_path.reason = 0;
+
+ res = parse_flags(&s[n], slow_path_reason_to_string,
+ &cookie.slow_path.reason);
+ if (res < 0) {
+ return res;
+ }
+ n += res;
+ if (s[n] != ')') {
+ return -EINVAL;
+ }
+ n++;
+
+ odp_put_userspace_action(pid, &cookie, sizeof cookie, actions);
+ return n;
+ } else if (sscanf(s, "userspace(pid=%lli,userdata="
+ "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
+ &n) > 0 && n > 0) {
+ uint64_t userdata;
+
+ userdata = strtoull(userdata_s, NULL, 0);
+ odp_put_userspace_action(pid, &userdata, sizeof(userdata),
+ actions);
+ return n;
+ } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
+ && n > 0) {
+ struct ofpbuf buf;
+ char *end;
+
+ ofpbuf_init(&buf, 16);
+ end = ofpbuf_put_hex(&buf, &s[n], NULL);
+ if (end[0] == ')' && end[1] == ')') {
+ odp_put_userspace_action(pid, buf.data, buf.size, actions);
+ ofpbuf_uninit(&buf);
+ return (end + 2) - s;
+ }
+ }
+ }
+
+ if (!strncmp(s, "set(", 4)) {
+ size_t start_ofs;
+ int retval;
+
+ start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
+ retval = parse_odp_key_attr(s + 4, port_names, actions);
+ if (retval < 0) {
+ return retval;
+ }
+ if (s[retval + 4] != ')') {
+ return -EINVAL;
+ }
+ nl_msg_end_nested(actions, start_ofs);
+ return retval + 5;
+ }
+
+ {
+ struct ovs_action_push_vlan push;
+ int tpid = ETH_TYPE_VLAN;
+ int vid, pcp;
+ int cfi = 1;
+ int n = -1;
+
+ if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
+ && n > 0)
+ || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
+ &vid, &pcp, &cfi, &n) > 0 && n > 0)
+ || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
+ &tpid, &vid, &pcp, &n) > 0 && n > 0)
+ || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
+ &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
+ push.vlan_tpid = htons(tpid);
+ push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
+ | (pcp << VLAN_PCP_SHIFT)
+ | (cfi ? VLAN_CFI : 0));
+ nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
+ &push, sizeof push);
+
+ return n;
+ }
+ }
+
+ if (!strncmp(s, "pop_vlan", 8)) {
+ nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
+ return 8;
+ }
+
+ {
+ double percentage;
+ int n = -1;
+
+ if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
+ && percentage >= 0. && percentage <= 100.0
+ && n > 0) {
+ size_t sample_ofs, actions_ofs;
+ double probability;
+
+ probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
+ sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
+ nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
+ (probability <= 0 ? 0
+ : probability >= UINT32_MAX ? UINT32_MAX
+ : probability));
+
+ actions_ofs = nl_msg_start_nested(actions,
+ OVS_SAMPLE_ATTR_ACTIONS);
+ for (;;) {
+ int retval;
+
+ n += strspn(s + n, delimiters);
+ if (s[n] == ')') {
+ break;
+ }
+
+ retval = parse_odp_action(s + n, port_names, actions);
+ if (retval < 0) {
+ return retval;
+ }
+ n += retval;
+ }
+ nl_msg_end_nested(actions, actions_ofs);
+ nl_msg_end_nested(actions, sample_ofs);
+
+ return s[n + 1] == ')' ? n + 2 : -EINVAL;
+ }
+ }
+
+ return -EINVAL;
+}
+
+/* Parses the string representation of datapath actions, in the format output
+ * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
+ * value. On success, the ODP actions are appended to 'actions' as a series of
+ * Netlink attributes. On failure, no data is appended to 'actions'. Either
+ * way, 'actions''s data might be reallocated. */
+int
+odp_actions_from_string(const char *s, const struct simap *port_names,
+ struct ofpbuf *actions)
+{
+ size_t old_size;
+
+ if (!strcasecmp(s, "drop")) {
+ return 0;
+ }
+
+ old_size = actions->size;
+ for (;;) {
+ int retval;
+
+ s += strspn(s, delimiters);
+ if (!*s) {
+ return 0;
+ }
+
+ retval = parse_odp_action(s, port_names, actions);
+ if (retval < 0 || !strchr(delimiters, s[retval])) {
+ actions->size = old_size;
+ return -retval;
+ }
+ s += retval;
+ }
+
+ return 0;
+}