~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/dpif-linux.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
     * the Netlink version of the command, even if actions_len is zero. */
106
106
    const struct nlattr *key;           /* OVS_FLOW_ATTR_KEY. */
107
107
    size_t key_len;
 
108
    const struct nlattr *mask;          /* OVS_FLOW_ATTR_MASK. */
 
109
    size_t mask_len;
108
110
    const struct nlattr *actions;       /* OVS_FLOW_ATTR_ACTIONS. */
109
111
    size_t actions_len;
110
112
    const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
138
140
    int dp_ifindex;
139
141
 
140
142
    /* Upcall messages. */
 
143
    struct ovs_mutex upcall_lock;
141
144
    int uc_array_size;          /* Size of 'channels' and 'epoll_events'. */
142
145
    struct dpif_channel *channels;
143
146
    struct epoll_event *epoll_events;
146
149
    int event_offset;           /* Offset into 'epoll_events'. */
147
150
 
148
151
    /* Change notification. */
149
 
    struct sset changed_ports;  /* Ports that have changed. */
150
 
    struct nln_notifier *port_notifier;
151
 
    bool change_error;
 
152
    struct nl_sock *port_notifier; /* vport multicast group subscriber. */
152
153
};
153
154
 
154
155
static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
155
156
 
156
 
/* Generic Netlink family numbers for OVS. */
 
157
/* Generic Netlink family numbers for OVS.
 
158
 *
 
159
 * Initialized by dpif_linux_init(). */
157
160
static int ovs_datapath_family;
158
161
static int ovs_vport_family;
159
162
static int ovs_flow_family;
160
163
static int ovs_packet_family;
161
164
 
162
 
/* Generic Netlink socket. */
163
 
static struct nl_sock *genl_sock;
164
 
static struct nln *nln = NULL;
 
165
/* Generic Netlink multicast groups for OVS.
 
166
 *
 
167
 * Initialized by dpif_linux_init(). */
 
168
static unsigned int ovs_vport_mcgroup;
165
169
 
166
170
static int dpif_linux_init(void);
167
 
static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
168
 
static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
169
 
static void dpif_linux_port_changed(const void *vport, void *dpif);
170
 
static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
 
171
static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
 
172
static uint32_t dpif_linux_port_get_pid(const struct dpif *,
 
173
                                        odp_port_t port_no);
171
174
 
172
175
static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
173
176
                                       struct ofpbuf *);
233
236
        return error;
234
237
    }
235
238
 
236
 
    open_dpif(&dp, dpifp);
 
239
    error = open_dpif(&dp, dpifp);
237
240
    ofpbuf_delete(buf);
238
 
    return 0;
 
241
    return error;
239
242
}
240
243
 
241
 
static void
 
244
static int
242
245
open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
243
246
{
244
247
    struct dpif_linux *dpif;
245
248
 
246
249
    dpif = xzalloc(sizeof *dpif);
247
 
    dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
248
 
                                              dpif);
 
250
    dpif->port_notifier = NULL;
 
251
    ovs_mutex_init(&dpif->upcall_lock, PTHREAD_MUTEX_DEFAULT);
249
252
    dpif->epoll_fd = -1;
250
253
 
251
254
    dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
252
255
              dp->dp_ifindex, dp->dp_ifindex);
253
256
 
254
257
    dpif->dp_ifindex = dp->dp_ifindex;
255
 
    sset_init(&dpif->changed_ports);
256
258
    *dpifp = &dpif->dpif;
 
259
 
 
260
    return 0;
257
261
}
258
262
 
259
263
static void
260
264
destroy_channels(struct dpif_linux *dpif)
261
265
{
262
 
    int i;
 
266
    unsigned int i;
263
267
 
264
268
    if (dpif->epoll_fd < 0) {
265
269
        return;
274
278
            continue;
275
279
        }
276
280
 
 
281
        epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
 
282
 
277
283
        /* Turn off upcalls. */
278
284
        dpif_linux_vport_init(&vport_request);
279
285
        vport_request.cmd = OVS_VPORT_CMD_SET;
280
286
        vport_request.dp_ifindex = dpif->dp_ifindex;
281
 
        vport_request.port_no = i;
 
287
        vport_request.port_no = u32_to_odp(i);
282
288
        vport_request.upcall_pid = &upcall_pid;
283
289
        dpif_linux_vport_transact(&vport_request, NULL, NULL);
284
290
 
293
299
    dpif->epoll_events = NULL;
294
300
    dpif->n_events = dpif->event_offset = 0;
295
301
 
296
 
    close(dpif->epoll_fd);
297
 
    dpif->epoll_fd = -1;
 
302
    /* Don't close dpif->epoll_fd since that would cause other threads that
 
303
     * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
298
304
}
299
305
 
300
306
static int
301
 
add_channel(struct dpif_linux *dpif, uint32_t port_no, struct nl_sock *sock)
 
307
add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
302
308
{
303
309
    struct epoll_event event;
 
310
    uint32_t port_idx = odp_to_u32(port_no);
304
311
 
305
312
    if (dpif->epoll_fd < 0) {
306
313
        return 0;
308
315
 
309
316
    /* We assume that the datapath densely chooses port numbers, which
310
317
     * can therefore be used as an index into an array of channels. */
311
 
    if (port_no >= dpif->uc_array_size) {
312
 
        int new_size = port_no + 1;
313
 
        int i;
 
318
    if (port_idx >= dpif->uc_array_size) {
 
319
        uint32_t new_size = port_idx + 1;
 
320
        uint32_t i;
314
321
 
315
 
        if (new_size > 65535) {
 
322
        if (new_size > MAX_PORTS) {
316
323
            VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
317
324
                         dpif_name(&dpif->dpif), port_no);
318
325
            return EFBIG;
331
338
 
332
339
    memset(&event, 0, sizeof event);
333
340
    event.events = EPOLLIN;
334
 
    event.data.u32 = port_no;
 
341
    event.data.u32 = port_idx;
335
342
    if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
336
343
                  &event) < 0) {
337
344
        return errno;
338
345
    }
339
346
 
340
 
    nl_sock_destroy(dpif->channels[port_no].sock);
341
 
    dpif->channels[port_no].sock = sock;
342
 
    dpif->channels[port_no].last_poll = LLONG_MIN;
 
347
    nl_sock_destroy(dpif->channels[port_idx].sock);
 
348
    dpif->channels[port_idx].sock = sock;
 
349
    dpif->channels[port_idx].last_poll = LLONG_MIN;
343
350
 
344
351
    return 0;
345
352
}
346
353
 
347
354
static void
348
 
del_channel(struct dpif_linux *dpif, uint32_t port_no)
 
355
del_channel(struct dpif_linux *dpif, odp_port_t port_no)
349
356
{
350
357
    struct dpif_channel *ch;
 
358
    uint32_t port_idx = odp_to_u32(port_no);
351
359
 
352
 
    if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
 
360
    if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
353
361
        return;
354
362
    }
355
363
 
356
 
    ch = &dpif->channels[port_no];
 
364
    ch = &dpif->channels[port_idx];
357
365
    if (!ch->sock) {
358
366
        return;
359
367
    }
370
378
{
371
379
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
372
380
 
373
 
    nln_notifier_destroy(dpif->port_notifier);
 
381
    nl_sock_destroy(dpif->port_notifier);
374
382
    destroy_channels(dpif);
375
 
    sset_destroy(&dpif->changed_ports);
 
383
    if (dpif->epoll_fd >= 0) {
 
384
        close(dpif->epoll_fd);
 
385
    }
 
386
    ovs_mutex_destroy(&dpif->upcall_lock);
376
387
    free(dpif);
377
388
}
378
389
 
388
399
    return dpif_linux_dp_transact(&dp, NULL, NULL);
389
400
}
390
401
 
391
 
static void
392
 
dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
393
 
{
394
 
    if (nln) {
395
 
        nln_run(nln);
396
 
    }
397
 
}
398
 
 
399
 
static void
400
 
dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
401
 
{
402
 
    if (nln) {
403
 
        nln_wait(nln);
404
 
    }
405
 
}
406
 
 
407
402
static int
408
403
dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
409
404
{
443
438
    case OVS_VPORT_TYPE_VXLAN:
444
439
        return "vxlan";
445
440
 
 
441
    case OVS_VPORT_TYPE_LISP:
 
442
        return "lisp";
 
443
 
446
444
    case OVS_VPORT_TYPE_UNSPEC:
447
445
    case __OVS_VPORT_TYPE_MAX:
448
446
        break;
468
466
        return OVS_VPORT_TYPE_GRE;
469
467
    } else if (!strcmp(type, "vxlan")) {
470
468
        return OVS_VPORT_TYPE_VXLAN;
 
469
    } else if (!strcmp(type, "lisp")) {
 
470
        return OVS_VPORT_TYPE_LISP;
471
471
    } else {
472
472
        return OVS_VPORT_TYPE_UNSPEC;
473
473
    }
474
474
}
475
475
 
476
476
static int
477
 
dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
478
 
                    uint32_t *port_nop)
 
477
dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
 
478
                      odp_port_t *port_nop)
479
479
{
480
480
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
481
481
    const struct netdev_tunnel_config *tnl_cfg;
482
 
    const char *name = netdev_vport_get_dpif_port(netdev);
 
482
    char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
 
483
    const char *name = netdev_vport_get_dpif_port(netdev,
 
484
                                                  namebuf, sizeof namebuf);
483
485
    const char *type = netdev_get_type(netdev);
484
486
    struct dpif_linux_vport request, reply;
485
487
    struct nl_sock *sock = NULL;
532
534
        VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
533
535
                 dpif_name(dpif_), reply.port_no, upcall_pid);
534
536
    } else {
535
 
        if (error == EBUSY && *port_nop != UINT32_MAX) {
 
537
        if (error == EBUSY && *port_nop != ODPP_NONE) {
536
538
            VLOG_INFO("%s: requested port %"PRIu32" is in use",
537
539
                      dpif_name(dpif_), *port_nop);
538
540
        }
564
566
}
565
567
 
566
568
static int
567
 
dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
 
569
dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
 
570
                    odp_port_t *port_nop)
 
571
{
 
572
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
573
    int error;
 
574
 
 
575
    ovs_mutex_lock(&dpif->upcall_lock);
 
576
    error = dpif_linux_port_add__(dpif_, netdev, port_nop);
 
577
    ovs_mutex_unlock(&dpif->upcall_lock);
 
578
 
 
579
    return error;
 
580
}
 
581
 
 
582
static int
 
583
dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
568
584
{
569
585
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
570
586
    struct dpif_linux_vport vport;
582
598
}
583
599
 
584
600
static int
585
 
dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
 
601
dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
 
602
{
 
603
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
604
    int error;
 
605
 
 
606
    ovs_mutex_lock(&dpif->upcall_lock);
 
607
    error = dpif_linux_port_del__(dpif_, port_no);
 
608
    ovs_mutex_unlock(&dpif->upcall_lock);
 
609
 
 
610
    return error;
 
611
}
 
612
 
 
613
static int
 
614
dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
586
615
                        const char *port_name, struct dpif_port *dpif_port)
587
616
{
588
617
    struct dpif_linux_vport request;
613
642
}
614
643
 
615
644
static int
616
 
dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
 
645
dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
617
646
                                struct dpif_port *dpif_port)
618
647
{
619
648
    return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
626
655
    return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
627
656
}
628
657
 
629
 
static int
 
658
static odp_port_t
630
659
dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
631
660
{
632
 
    return MAX_PORTS;
 
661
    return u32_to_odp(MAX_PORTS);
633
662
}
634
663
 
635
664
static uint32_t
636
 
dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
 
665
dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
637
666
{
638
667
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
668
    uint32_t port_idx = odp_to_u32(port_no);
 
669
    uint32_t pid = 0;
639
670
 
640
 
    if (dpif->epoll_fd < 0) {
641
 
        return 0;
642
 
    } else {
643
 
        /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
 
671
    ovs_mutex_lock(&dpif->upcall_lock);
 
672
    if (dpif->epoll_fd >= 0) {
 
673
        /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
644
674
         * channel, since it is not heavily loaded. */
645
 
        int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
646
 
        return nl_sock_pid(dpif->channels[idx].sock);
 
675
        uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
 
676
        pid = nl_sock_pid(dpif->channels[idx].sock);
647
677
    }
 
678
    ovs_mutex_unlock(&dpif->upcall_lock);
 
679
 
 
680
    return pid;
648
681
}
649
682
 
650
683
static int
651
684
dpif_linux_flow_flush(struct dpif *dpif_)
652
685
{
653
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
686
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
654
687
    struct dpif_linux_flow flow;
655
688
 
656
689
    dpif_linux_flow_init(&flow);
666
699
static int
667
700
dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
668
701
{
669
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
702
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
670
703
    struct dpif_linux_port_state *state;
671
704
    struct dpif_linux_vport request;
672
705
    struct ofpbuf *buf;
679
712
 
680
713
    buf = ofpbuf_new(1024);
681
714
    dpif_linux_vport_to_ofpbuf(&request, buf);
682
 
    nl_dump_start(&state->dump, genl_sock, buf);
 
715
    nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
683
716
    ofpbuf_delete(buf);
684
717
 
685
718
    return 0;
724
757
{
725
758
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
726
759
 
727
 
    if (dpif->change_error) {
728
 
        dpif->change_error = false;
729
 
        sset_clear(&dpif->changed_ports);
730
 
        return ENOBUFS;
731
 
    } else if (!sset_is_empty(&dpif->changed_ports)) {
732
 
        *devnamep = sset_pop(&dpif->changed_ports);
733
 
        return 0;
734
 
    } else {
735
 
        return EAGAIN;
 
760
    /* Lazily create the Netlink socket to listen for notifications. */
 
761
    if (!dpif->port_notifier) {
 
762
        struct nl_sock *sock;
 
763
        int error;
 
764
 
 
765
        error = nl_sock_create(NETLINK_GENERIC, &sock);
 
766
        if (error) {
 
767
            return error;
 
768
        }
 
769
 
 
770
        error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
 
771
        if (error) {
 
772
            nl_sock_destroy(sock);
 
773
            return error;
 
774
        }
 
775
        dpif->port_notifier = sock;
 
776
 
 
777
        /* We have no idea of the current state so report that everything
 
778
         * changed. */
 
779
        return ENOBUFS;
 
780
    }
 
781
 
 
782
    for (;;) {
 
783
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 
784
        uint64_t buf_stub[4096 / 8];
 
785
        struct ofpbuf buf;
 
786
        int error;
 
787
 
 
788
        ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
 
789
        error = nl_sock_recv(dpif->port_notifier, &buf, false);
 
790
        if (!error) {
 
791
            struct dpif_linux_vport vport;
 
792
 
 
793
            error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
 
794
            if (!error) {
 
795
                if (vport.dp_ifindex == dpif->dp_ifindex
 
796
                    && (vport.cmd == OVS_VPORT_CMD_NEW
 
797
                        || vport.cmd == OVS_VPORT_CMD_DEL
 
798
                        || vport.cmd == OVS_VPORT_CMD_SET)) {
 
799
                    VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
 
800
                             dpif->dpif.full_name, vport.name, vport.cmd);
 
801
                    *devnamep = xstrdup(vport.name);
 
802
                    return 0;
 
803
                } else {
 
804
                    continue;
 
805
                }
 
806
            }
 
807
        } else if (error == EAGAIN) {
 
808
            return EAGAIN;
 
809
        }
 
810
 
 
811
        VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
 
812
                     ovs_strerror(error));
 
813
        nl_sock_drain(dpif->port_notifier);
 
814
        return ENOBUFS;
736
815
    }
737
816
}
738
817
 
739
818
static void
740
819
dpif_linux_port_poll_wait(const struct dpif *dpif_)
741
820
{
742
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
743
 
    if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
 
821
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
822
 
 
823
    if (dpif->port_notifier) {
 
824
        nl_sock_wait(dpif->port_notifier, POLLIN);
 
825
    } else {
744
826
        poll_immediate_wake();
745
827
    }
746
828
}
750
832
                      const struct nlattr *key, size_t key_len,
751
833
                      struct dpif_linux_flow *reply, struct ofpbuf **bufp)
752
834
{
753
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
835
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
754
836
    struct dpif_linux_flow request;
755
837
 
756
838
    dpif_linux_flow_init(&request);
790
872
dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
791
873
                         struct dpif_linux_flow *request)
792
874
{
793
 
    static struct nlattr dummy_action;
 
875
    static const struct nlattr dummy_action;
794
876
 
795
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
877
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
796
878
 
797
879
    dpif_linux_flow_init(request);
798
880
    request->cmd = (put->flags & DPIF_FP_CREATE
800
882
    request->dp_ifindex = dpif->dp_ifindex;
801
883
    request->key = put->key;
802
884
    request->key_len = put->key_len;
 
885
    request->mask = put->mask;
 
886
    request->mask_len = put->mask_len;
803
887
    /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
804
 
    request->actions = put->actions ? put->actions : &dummy_action;
 
888
    request->actions = (put->actions
 
889
                        ? put->actions
 
890
                        : CONST_CAST(struct nlattr *, &dummy_action));
805
891
    request->actions_len = put->actions_len;
806
892
    if (put->flags & DPIF_FP_ZERO_STATS) {
807
893
        request->clear = true;
831
917
dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
832
918
                         struct dpif_linux_flow *request)
833
919
{
834
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
920
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
835
921
 
836
922
    dpif_linux_flow_init(request);
837
923
    request->cmd = OVS_FLOW_CMD_DEL;
868
954
static int
869
955
dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
870
956
{
871
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
957
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
872
958
    struct dpif_linux_flow_state *state;
873
959
    struct dpif_linux_flow request;
874
960
    struct ofpbuf *buf;
881
967
 
882
968
    buf = ofpbuf_new(1024);
883
969
    dpif_linux_flow_to_ofpbuf(&request, buf);
884
 
    nl_dump_start(&state->dump, genl_sock, buf);
 
970
    nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
885
971
    ofpbuf_delete(buf);
886
972
 
887
973
    state->buf = NULL;
892
978
static int
893
979
dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
894
980
                          const struct nlattr **key, size_t *key_len,
 
981
                          const struct nlattr **mask, size_t *mask_len,
895
982
                          const struct nlattr **actions, size_t *actions_len,
896
983
                          const struct dpif_flow_stats **stats)
897
984
{
919
1006
            if (error == ENOENT) {
920
1007
                VLOG_DBG("dumped flow disappeared on get");
921
1008
            } else if (error) {
922
 
                VLOG_WARN("error fetching dumped flow: %s", strerror(error));
 
1009
                VLOG_WARN("error fetching dumped flow: %s",
 
1010
                          ovs_strerror(error));
923
1011
            }
924
1012
        }
925
1013
    } while (error);
932
1020
        *key = state->flow.key;
933
1021
        *key_len = state->flow.key_len;
934
1022
    }
 
1023
    if (mask) {
 
1024
        *mask = state->flow.mask;
 
1025
        *mask_len = state->flow.mask ? state->flow.mask_len : 0;
 
1026
    }
935
1027
    if (stats) {
936
1028
        dpif_linux_flow_get_stats(&state->flow, &state->stats);
937
1029
        *stats = &state->stats;
982
1074
 
983
1075
    ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
984
1076
    dpif_linux_encode_execute(dp_ifindex, execute, &request);
985
 
    error = nl_sock_transact(genl_sock, &request, NULL);
 
1077
    error = nl_transact(NETLINK_GENERIC, &request, NULL);
986
1078
    ofpbuf_uninit(&request);
987
1079
 
988
1080
    return error;
991
1083
static int
992
1084
dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
993
1085
{
994
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
1086
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
995
1087
 
996
1088
    return dpif_linux_execute__(dpif->dp_ifindex, execute);
997
1089
}
1001
1093
static void
1002
1094
dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1003
1095
{
1004
 
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
1096
    const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1005
1097
 
1006
1098
    struct op_auxdata {
1007
1099
        struct nl_transaction txn;
1067
1159
    for (i = 0; i < n_ops; i++) {
1068
1160
        txnsp[i] = &auxes[i].txn;
1069
1161
    }
1070
 
    nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
 
1162
    nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1071
1163
 
1072
1164
    for (i = 0; i < n_ops; i++) {
1073
1165
        struct op_auxdata *aux = &auxes[i];
1141
1233
}
1142
1234
 
1143
1235
static int
1144
 
dpif_linux_recv_set(struct dpif *dpif_, bool enable)
 
1236
dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1145
1237
{
1146
1238
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1147
1239
 
1155
1247
        struct dpif_port_dump port_dump;
1156
1248
        struct dpif_port port;
1157
1249
 
1158
 
        dpif->epoll_fd = epoll_create(10);
1159
1250
        if (dpif->epoll_fd < 0) {
1160
 
            return errno;
 
1251
            dpif->epoll_fd = epoll_create(10);
 
1252
            if (dpif->epoll_fd < 0) {
 
1253
                return errno;
 
1254
            }
1161
1255
        }
1162
1256
 
1163
1257
        DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1186
1280
            } else {
1187
1281
                VLOG_WARN_RL(&error_rl,
1188
1282
                             "%s: failed to set upcall pid on port: %s",
1189
 
                             dpif_name(&dpif->dpif), strerror(error));
 
1283
                             dpif_name(&dpif->dpif), ovs_strerror(error));
1190
1284
                nl_sock_destroy(sock);
1191
1285
 
1192
1286
                if (error == ENODEV || error == ENOENT) {
1211
1305
}
1212
1306
 
1213
1307
static int
 
1308
dpif_linux_recv_set(struct dpif *dpif_, bool enable)
 
1309
{
 
1310
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
1311
    int error;
 
1312
 
 
1313
    ovs_mutex_lock(&dpif->upcall_lock);
 
1314
    error = dpif_linux_recv_set__(dpif_, enable);
 
1315
    ovs_mutex_unlock(&dpif->upcall_lock);
 
1316
 
 
1317
    return error;
 
1318
}
 
1319
 
 
1320
static int
1214
1321
dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1215
1322
                             uint32_t queue_id, uint32_t *priority)
1216
1323
{
1233
1340
        [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1234
1341
 
1235
1342
        /* OVS_PACKET_CMD_ACTION only. */
1236
 
        [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
 
1343
        [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1237
1344
    };
1238
1345
 
1239
1346
    struct ovs_header *ovs_header;
1271
1378
    upcall->key = CONST_CAST(struct nlattr *,
1272
1379
                             nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1273
1380
    upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1274
 
    upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1275
 
                        ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1276
 
                        : 0);
 
1381
    upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1277
1382
    *dp_ifindex = ovs_header->dp_ifindex;
1278
1383
 
1279
1384
    return 0;
1280
1385
}
1281
1386
 
1282
1387
static int
1283
 
dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1284
 
                struct ofpbuf *buf)
 
1388
dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
 
1389
                  struct ofpbuf *buf)
1285
1390
{
1286
1391
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1287
1392
    int read_tries = 0;
1301
1406
        } while (retval < 0 && errno == EINTR);
1302
1407
        if (retval < 0) {
1303
1408
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1304
 
            VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
 
1409
            VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1305
1410
        } else if (retval > 0) {
1306
1411
            dpif->n_events = retval;
1307
1412
        }
1351
1456
    return EAGAIN;
1352
1457
}
1353
1458
 
 
1459
static int
 
1460
dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
 
1461
                struct ofpbuf *buf)
 
1462
{
 
1463
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
1464
    int error;
 
1465
 
 
1466
    ovs_mutex_lock(&dpif->upcall_lock);
 
1467
    error = dpif_linux_recv__(dpif_, upcall, buf);
 
1468
    ovs_mutex_unlock(&dpif->upcall_lock);
 
1469
 
 
1470
    return error;
 
1471
}
 
1472
 
1354
1473
static void
1355
1474
dpif_linux_recv_wait(struct dpif *dpif_)
1356
1475
{
1357
1476
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1358
1477
 
1359
 
    if (dpif->epoll_fd < 0) {
1360
 
       return;
 
1478
    ovs_mutex_lock(&dpif->upcall_lock);
 
1479
    if (dpif->epoll_fd >= 0) {
 
1480
        poll_fd_wait(dpif->epoll_fd, POLLIN);
1361
1481
    }
1362
 
 
1363
 
    poll_fd_wait(dpif->epoll_fd, POLLIN);
 
1482
    ovs_mutex_unlock(&dpif->upcall_lock);
1364
1483
}
1365
1484
 
1366
1485
static void
1367
1486
dpif_linux_recv_purge(struct dpif *dpif_)
1368
1487
{
1369
1488
    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1370
 
    struct dpif_channel *ch;
1371
 
 
1372
 
    if (dpif->epoll_fd < 0) {
1373
 
       return;
1374
 
    }
1375
 
 
1376
 
    for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1377
 
        if (ch->sock) {
1378
 
            nl_sock_drain(ch->sock);
 
1489
 
 
1490
    ovs_mutex_lock(&dpif->upcall_lock);
 
1491
    if (dpif->epoll_fd >= 0) {
 
1492
        struct dpif_channel *ch;
 
1493
 
 
1494
        for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
 
1495
             ch++) {
 
1496
            if (ch->sock) {
 
1497
                nl_sock_drain(ch->sock);
 
1498
            }
1379
1499
        }
1380
1500
    }
 
1501
    ovs_mutex_unlock(&dpif->upcall_lock);
1381
1502
}
1382
1503
 
1383
1504
const struct dpif_class dpif_linux_class = {
1387
1508
    dpif_linux_open,
1388
1509
    dpif_linux_close,
1389
1510
    dpif_linux_destroy,
1390
 
    dpif_linux_run,
1391
 
    dpif_linux_wait,
 
1511
    NULL,                       /* run */
 
1512
    NULL,                       /* wait */
1392
1513
    dpif_linux_get_stats,
1393
1514
    dpif_linux_port_add,
1394
1515
    dpif_linux_port_del,
1420
1541
static int
1421
1542
dpif_linux_init(void)
1422
1543
{
1423
 
    static int error = -1;
1424
 
 
1425
 
    if (error < 0) {
1426
 
        unsigned int ovs_vport_mcgroup;
1427
 
 
 
1544
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
 
1545
    static int error;
 
1546
 
 
1547
    if (ovsthread_once_start(&once)) {
1428
1548
        error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1429
1549
                                      &ovs_datapath_family);
1430
1550
        if (error) {
1443
1563
                                          &ovs_packet_family);
1444
1564
        }
1445
1565
        if (!error) {
1446
 
            error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1447
 
        }
1448
 
        if (!error) {
1449
1566
            error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1450
1567
                                           &ovs_vport_mcgroup,
1451
1568
                                           OVS_VPORT_MCGROUP_FALLBACK_ID);
1452
1569
        }
1453
 
        if (!error) {
1454
 
            static struct dpif_linux_vport vport;
1455
 
            nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1456
 
                             dpif_linux_nln_parse, &vport);
1457
 
        }
 
1570
 
 
1571
        ovsthread_once_done(&once);
1458
1572
    }
1459
1573
 
1460
1574
    return error;
1472
1586
        ofpbuf_delete(buf);
1473
1587
    } else if (error != ENODEV && error != ENOENT) {
1474
1588
        VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1475
 
                     name, strerror(error));
 
1589
                     name, ovs_strerror(error));
1476
1590
    }
1477
1591
 
1478
1592
    return reply.type == OVS_VPORT_TYPE_INTERNAL;
1479
1593
}
1480
 
 
1481
 
static bool
1482
 
dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1483
 
{
1484
 
    struct dpif_linux_vport *vport = vport_;
1485
 
    return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1486
 
}
1487
 
 
1488
 
static void
1489
 
dpif_linux_port_changed(const void *vport_, void *dpif_)
1490
 
{
1491
 
    const struct dpif_linux_vport *vport = vport_;
1492
 
    struct dpif_linux *dpif = dpif_;
1493
 
 
1494
 
    if (vport) {
1495
 
        if (vport->dp_ifindex == dpif->dp_ifindex
1496
 
            && (vport->cmd == OVS_VPORT_CMD_NEW
1497
 
                || vport->cmd == OVS_VPORT_CMD_DEL
1498
 
                || vport->cmd == OVS_VPORT_CMD_SET)) {
1499
 
            VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1500
 
                     dpif->dpif.full_name, vport->name, vport->cmd);
1501
 
            sset_add(&dpif->changed_ports, vport->name);
1502
 
        }
1503
 
    } else {
1504
 
        dpif->change_error = true;
1505
 
    }
1506
 
}
1507
1594
 
1508
1595
/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1509
1596
 * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1546
1633
 
1547
1634
    vport->cmd = genl->cmd;
1548
1635
    vport->dp_ifindex = ovs_header->dp_ifindex;
1549
 
    vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
 
1636
    vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1550
1637
    vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1551
1638
    vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1552
1639
    if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1576
1663
    ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1577
1664
    ovs_header->dp_ifindex = vport->dp_ifindex;
1578
1665
 
1579
 
    if (vport->port_no != UINT32_MAX) {
1580
 
        nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
 
1666
    if (vport->port_no != ODPP_NONE) {
 
1667
        nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1581
1668
    }
1582
1669
 
1583
1670
    if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1608
1695
dpif_linux_vport_init(struct dpif_linux_vport *vport)
1609
1696
{
1610
1697
    memset(vport, 0, sizeof *vport);
1611
 
    vport->port_no = UINT32_MAX;
 
1698
    vport->port_no = ODPP_NONE;
1612
1699
}
1613
1700
 
1614
1701
/* Executes 'request' in the kernel datapath.  If the command fails, returns a
1638
1725
 
1639
1726
    request_buf = ofpbuf_new(1024);
1640
1727
    dpif_linux_vport_to_ofpbuf(request, request_buf);
1641
 
    error = nl_sock_transact(genl_sock, request_buf, bufp);
 
1728
    error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1642
1729
    ofpbuf_delete(request_buf);
1643
1730
 
1644
1731
    if (reply) {
1759
1846
 
1760
1847
    buf = ofpbuf_new(1024);
1761
1848
    dpif_linux_dp_to_ofpbuf(&request, buf);
1762
 
    nl_dump_start(dump, genl_sock, buf);
 
1849
    nl_dump_start(dump, NETLINK_GENERIC, buf);
1763
1850
    ofpbuf_delete(buf);
1764
1851
}
1765
1852
 
1780
1867
 
1781
1868
    request_buf = ofpbuf_new(1024);
1782
1869
    dpif_linux_dp_to_ofpbuf(request, request_buf);
1783
 
    error = nl_sock_transact(genl_sock, request_buf, bufp);
 
1870
    error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1784
1871
    ofpbuf_delete(request_buf);
1785
1872
 
1786
1873
    if (reply) {
1825
1912
{
1826
1913
    static const struct nl_policy ovs_flow_policy[] = {
1827
1914
        [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
 
1915
        [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1828
1916
        [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1829
1917
        [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1830
1918
                                  .optional = true },
1856
1944
    flow->dp_ifindex = ovs_header->dp_ifindex;
1857
1945
    flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1858
1946
    flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
 
1947
 
 
1948
    if (a[OVS_FLOW_ATTR_MASK]) {
 
1949
        flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
 
1950
        flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
 
1951
    }
1859
1952
    if (a[OVS_FLOW_ATTR_ACTIONS]) {
1860
1953
        flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1861
1954
        flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1891
1984
        nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1892
1985
    }
1893
1986
 
 
1987
    if (flow->mask_len) {
 
1988
        nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
 
1989
    }
 
1990
 
1894
1991
    if (flow->actions || flow->actions_len) {
1895
1992
        nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1896
1993
                          flow->actions, flow->actions_len);
1934
2031
 
1935
2032
    request_buf = ofpbuf_new(1024);
1936
2033
    dpif_linux_flow_to_ofpbuf(request, request_buf);
1937
 
    error = nl_sock_transact(genl_sock, request_buf, bufp);
 
2034
    error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1938
2035
    ofpbuf_delete(request_buf);
1939
2036
 
1940
2037
    if (reply) {